[英]Guess the number game in python 3
so I'm making a program where the user has 20 guesses to guess a random integer 1-1000.所以我正在制作一个程序,用户有 20 次猜测来猜测随机 integer 1-1000。 there are 2 different spots where I'm hung up.我在两个不同的地方挂了电话。 1- im having a hard time getting their y/n input as part of the while loop, I want it to end when they say N/n and I want the loop to start when they say Y/y. 1- 我很难将他们的 y/n 输入作为 while 循环的一部分,我希望它在他们说 N/n 时结束,我希望循环在他们说 Y/y 时开始。 2- in my print function that says 'please enter guess #', I have been trying to figure out how to get it to print the actual guess number that they are on seeing as they only get 20 guesses but I am having syntax errors 2-在我的打印 function 中说“请输入猜测 #”,我一直在试图弄清楚如何让它打印他们看到的实际猜测数字,因为他们只得到 20 个猜测,但我有语法错误
import random
guess = 0
number = random.randint(1,1000)
print(' Do you wish to play 20 guesses? (Y/N) ')
y_n = input ()
if y_n == 'y''Y':
continue继续
elif a=='n''N':
break
while guess < 20:
print('please enter guess ',', guessestaken )
guess=input()
guess=int(guess)
guessestatken = guess + 1
if guess < number:
print('Your guess is too low')
if guess > number:
print('Your guess is too high')
if guess==number:
break
if guess == number:
guess = str(guessestaken)
print('CONGRATGULATIONS! You WIN ! ! !')
if guess !=number:
number=str(number)
print('Nope, the number i was thinking of was ', number, '. You Lose')
So code can be cleaned to the following working version.因此可以将代码清理为以下工作版本。
import random
while True:
# Output loop checks if we want to continue to play
y_n = input ('Do you wish to play 20 guesses? (Y/N)')
if y_n != 'y' and y_n != 'Y':
print('Okay, ending guessing game')
break
number = random.randint(1,1000)
# for Loop to allow up to 20 guesses
for guesses_taken in range(20):
# Input allows an argument to show user prompt (no need for print prior to input)
guess =input(f'Please enter guess (guesses so far {guesses_taken})')
guess = int(guess)
if guess < number:
print('Your guess is too low')
elif guess > number:
print('Your guess is too high')
else:
print('CONGRATGULATIONS! You WIN ! ! !')
break
else:
print('Sorry, out of guesses')
You are using some redundant checks.您正在使用一些冗余检查。 Firstly, continue/break
statements are meant to be used within loops to break out of it or pass to next iteration, so using them inside if/else will do no good.首先, continue/break
语句用于在循环中打破它或传递到下一次迭代,因此在 if/else 中使用它们将没有好处。 Secondly, you never increase your guess
variable so it will never break out of loop.其次,你永远不会增加你的guess
变量,所以它永远不会跳出循环。 You need to increment it in each iteration.您需要在每次迭代中增加它。 Final code I came up with is:我想出的最终代码是:
import random
guess = 0
number = random.randint(1,1000)
print('Do you wish to play 20 guesses? (Y/N) ')
y_n = input ()
if y_n == 'y' or y_n == 'Y': # You need to use or to check for 2 different conditions
while guess < 20:
print('please enter guess ')
guessestatken = int(input())
if guessestatken < number:
print('Your guess is too low')
elif guessestatken > number:
print('Your guess is too high')
else:
print('CONGRATGULATIONS! You WIN ! ! !')
break
guess += 1
# guess being 20 means user never found correct number
if guess == 20:
print('Nope, the number i was thinking of was ', str(number), '. You Lose')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.