# 猜数字游戏Python（while循环）

#### Guess the number game Python (while loop)

``````import random
def main():
again='y'
count=0

while again=='y':
print('I have a number between 1 to 1000.')
print('Can you guess my number?')

number=random.randint(1, 1000)
print(number)

guess=int(input(''))

while guess !='':
if  guess>number:
print('Too high, try again!')
count+=1
print('count:',count)
guess=int(input(''))

elif guess<number:
print('Too low, try again!')
count+=1
print('count:',count)
guess=int(input(''))

elif guess==number:
print('Excellent!! You guessed the number!!!!')
print('Would you like to try again? (y or n)')
count+=1
print('count:',count)
again=str(input(''))

else:
print('You entered an invalid value')

main()
``````
2 个回复

``````elif guess==number:
print('Excellent!! You guessed the number!!!!')
print('Would you like to try again? (y or n)')
count+=1
print('count:',count)
again=str(input(''))
break # added this `break`, it will break the inner loop
``````

``````        elif guess==number:
print('Excellent!! You guessed the number!!!!')
print('Would you like to try again? (y or n)')
count+=1
print('count:',count)
again=str(input(''))
guess=''
``````

``````        elif guess==number:
print('Excellent!! You guessed the number!!!!')
print('Would you like to try again? (y or n)')
count+=1
print('count:',count)
again=str(input(''))
break
``````
1 猜数字游戏中的while循环

2 Python中的猜数字游戏的控制循环

3 使用While循环和函数进行猜数字游戏

4 猜数字游戏Python

2020-05-30 05:17:18 4 292   python
5 我的Python猜数字游戏

2016-07-08 10:29:18 3 153   python
6 猜游戏，带有While循环

7 Python 猜 2 数字游戏

2020-10-27 13:02:18 1 274   python
8 猜数字游戏python

9 Python猜数字游戏

10 Python：猜数字游戏

2014-01-28 21:26:33 4 10699   function