Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
224 views
Welcome To Ask or Share your Answers For Others

1 Answer

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...