2012-12-23 109 views
-1

我试图让变量number增加,如果用户猜对了正确的数字!如果被另一个用户猜测,则继续增加增加的一个。但这似乎是我的syntax是错误的。所以我真的需要你的帮助。贝娄是我的代码:Python可变增量while循环

#!/usr/bin/python 
# Filename: while.py 
number = 23 
running = True 

while running: 
    guess = int(raw_input('Enter an integer : ')) 

    if guess == number: 
     print 'Congratulations, you guessed it. The number is now increase' 
     number += 1 # Increase this so the next user won't know it! 
     running = False # this causes the while loop to stop 
    elif guess < number: 
     print 'No, it is a little higher than that.' 
    else: 
     print 'No, it is a little lower than that.' 
else: 
    print 'The while loop is over.' 
    # Do anything else you want to do here 
print 'Done' 
+0

by'syntax error'你的意思是语义错误吗? –

+0

我没有看到你的语法错误......也许你想循环继续猜猜数字后继续? – Joril

回答

-1
#!/usr/bin/python 
# Filename: while.py 
number = 23 
running = True 

while running: 
    guess = int(raw_input('Enter an integer : ')) 

    if guess == number: 
     print 'Congratulations, you guessed it. The number is now increase' 
     number += 1 # Increase this so the next user won't know it! 
     running = False # this causes the while loop to stop 
    elif guess < number: 
     print 'No, it is a little higher than that.' 
    else: 
     print 'No, it is a little lower than that.' 

# Do anything else you want to do here 
print 'Done' 
+4

在python中没有'else if'这样的东西;两个'别的'都没有顺序 – Rubens

1

你可以不用“跑”变量,它不需要

#!/usr/bin/python 
# Filename: while.py 
number = 23 
import sys 

try: 
    while True: 
     guess = int(raw_input('Enter an integer : ')) 
     if guess == number: 
      print('Congratulations, you guessed it. The number is now increase') 
      number += 1 # Increase this so the next user won't know it! 
     elif guess < number: 
      print('No, it is a little higher than that.') 
     else: 
      print('No, it is a little lower than that.') 
except ValueError: 
    print('Please write number') 
except KeyboardInterrupt: 
    sys.exit("Ok, you're finished with your game") 
-1

我不正常,你想做什么都知道,因为我初学者。但我知道你的代码中有一件事是错误的,那就是你在while循环后输入了其他东西。你的代码实际上没有意义,或者你的缩进是错误的,或者它是一个错误的代码,因为只有在你使用if之后才能输入。