import random # guess = random.randint(1,101) # print("I'm thinking of a number! Try to guess the number I'm thinking of:") while 1: guess = random.randint(1, 100) print("I'm thinking of a number! Try to guess the number I'm thinking of:") while guess: # a = int(input()) a = input() if a.isdigit(): a = int(a) if a < guess: print("Too low! Guess again:") elif a > guess: print("Too high! Guess again:") elif a == guess: print("That's it! Would you like to play again?(Yes/No)") break else: print("Please Enter a number") # print("Please Enter Yes or No:") # confirm = input() print("Please Enter Yes or No:") confirm = input() if confirm == 'Yes': continue elif confirm == 'No': print('Thanks for playing!') exit() else: while True: print("please Enter again:") confirm = input() if confirm == 'Yes': break elif confirm == 'No': print('Thanks for playing!') exit()
#修正:
#1、忽略了python对齐原则以为三层嵌套循环
#2、continue用法不熟悉
import random
# guess = random.randint(1,101)
# print("I'm thinking of a number! Try to guess the number I'm thinking of:")
while True:
guess = random.randint(1, 100)
print("I'm thinking of a number! Try to guess the number I'm thinking of:")
while guess:
# a = int(input())
a = input()
if a.isdigit():
a = int(a)
if a < guess:
print("Too low! Guess again:")
elif a > guess:
print("Too high! Guess again:")
elif a == guess:
print("That's it! Would you like to play again?(Yes/No)")
break
else:
print("Please Enter a number")
# print("Please Enter Yes or No:")
# confirm = input()
while True:
print("Please Enter Yes or No:")
confirm = input()
if confirm == 'Yes':
break
elif confirm == 'No':
print('Thanks for playing!')
exit()
else:
print("please Enter again:")
continue
继续改良版:2018-07-11
#今天发现了while循环增加标志 用布尔变量作为标志,判断循环是否可以继续
import random active = True while active: guess = random.randint(1, 100) print("I'm thinking of a number! Try to guess the number I'm thinking of:") while guess: a = input() if a.isdigit(): a = int(a) if a < guess: print("Too low! Guess again:") elif a > guess: print("Too high! Guess again:") elif a == guess: print("That's it! Would you like to play again?(Yes/No)") break else: print("Please Enter a number") while active: print("Please Enter Yes or No:") confirm = input() if confirm == 'Yes': break elif confirm == 'No': print('Thanks for playing!') active = False else: continue