Python variable increment while-loop

2019-09-20 23:29发布

I'm trying to make the variable number increase if user guessed the right number! And continue increase the increased one if it is guessed by another user. But it's seem my syntax is wrong. So i really need your help. Bellow is my code:

#!/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条回答
干净又极端
2楼-- · 2019-09-20 23:45

You can do it without "running" variable, it's not needed

#!/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")
查看更多
甜甜的少女心
3楼-- · 2019-09-20 23:52

i don't know properly what you want to do , as i am a beginner . but i know one thing that is wrong in your code is that you have typed else after a while loop . your code doesn't make sense actually , either your indentation is wrong or it is a wrong code as else is only typed after you use if.

查看更多
时光不老,我们不散
4楼-- · 2019-09-20 23:58
#!/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'
查看更多
登录 后发表回答