简体   繁体   English

从python的猜谜游戏中,我无法让自己找到正确的游戏

[英]From a guessing game in python, I can't get myself to get the correct one

This is my code: 这是我的代码:

 from sys import exit import random number = random.randint(1, 10) count = 0 def guess(): print ("Input a number 1 - 10") guess = input() if guess == "I give up": print ("The correct number was", number,"!") print ("You tried", count, "times before giving up!") exit(0) else: if guess == (number): print ("CORRECT!") print (count, "failed attempts.") exit(0) else: print ("WRONG!") print ("Try again!") global count count += 1 while True: guess() 

If I run this, I can keep guessing but never get the correct number. 如果我运行此命令,则可以继续猜测,但永远无法获得正确的数字。 I said I gave up, and it gave me the correct number. 我说我放弃了,它给了我正确的号码。 But I already guessed that number, so I don't know what's the problem. 但是我已经猜到了这个数字,所以我不知道出了什么问题。

You are taking the input as a string rather than an integer. 您将输入作为字符串而不是整数。 Thus, you may be comparing "5" to 5 , which is false. 因此,您可能会将"5"5进行比较,这是错误的。 Instead, call if int(guess) == (number) 相反, if int(guess) == (number)调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM