attempts = 0
guess = 5
name = input("what is your name?: ")
print("Hello",name,"i am thinking of a number")
print("it is between 1-20")
user_Guess = int(input("can you guess what it is?: "))
while user_Guess <5:
print("Too low!")
attempts += 1
while user_Guess >5:
print("too high!")
attempts += 1
即时通讯使用最新版本的python,我不知道我哪里出错了。当我输入正确的答案“5”时,它打印出来的太低!!!!我该怎么办?!请尽可能帮助,但不要过于复杂或彻底改变我的答案。
答案 0 :(得分:1)
好的,首先,如果你想要一个更干净的解决版本我做了一个。在尝试自己之前不要检查它,但请保留链接:https://repl.it/EQOg/2
如果你提供它,你当前的代码什么都不做.5。没有理由发出“太低”的消息。
但是,如果你尝试任何不是(包括20以上的数字),你将得到一个无限循环,因为用户没有机会修复任何东西。看到代码在另一行之后运行一行,所以说如果我猜3,那么这个循环:
while user_Guess <5:
print("Too low!")
将继续下去,因为user_Guess将总是低于5.您要做的是只有一个 while
循环,这将打破当用户猜对了。一般提示,当您尝试构建这样的逻辑内容时,编写描述您要实现的内容的伪代码会很有帮助。在这种情况下,您可能需要这样的东西:
#take number input from user
while users guess is wrong:
if the number is higher:
# print something & count attempt
if the number is lower:
# print something & count attempt
if the number is invalid:
# print something
# take number input from user (again)
## this is the important part - it lets the user change his guess.
## If his guess is correct, it will break the loop.
## If not, he gets to try again and again until correct
# print some success message after breaking the loop
有更聪明的设计,但这是我认为最简单的