Python变量while循环

时间:2012-12-23 13:00:28

标签: python variables loops numbers while-loop

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

#!/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'

3 个答案:

答案 0 :(得分: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 :(得分:-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'

答案 2 :(得分:-1)

我不知道你想做什么,因为我是初学者。但我知道你的代码中有一件事是你在while循环后输入了else。你的代码实际上没有意义,你的缩进是错误的还是错误的代码,因为只有你使用if后才输入。