【问题标题】:Guess the number game in python 3在python 3中猜数字游戏
【发布时间】:2021-05-18 14:08:39
【问题描述】:

所以我正在制作一个程序,其中用户有 20 次猜测来猜测 1-1000 的随机整数。我挂了 2 个不同的位置。 1- 我很难将他们的 y/n 输入作为 while 循环的一部分,我希望它在他们说 N/n 时结束,我希望循环在他们说 Y/y 时开始。 2-在我的打印函数中说'请输入猜测#',我一直在试图弄清楚如何让它打印他们看到的实际猜测数字,因为他们只得到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':

继续

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')

【问题讨论】:

    标签: python python-3.x loops input while-loop


    【解决方案1】:

    所以可以将代码清理为以下工作版本。

    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')
    

    【讨论】:

    • 如果我将范围设为 1,20,那就太完美了,因为猜测的数量不能真正为 0
    • @DelaneyMcNeese——它将允许 20 次猜测。当他们没有做出任何猜测时,猜测的次数为零。将范围设置为 range(20) 可以正确跟踪您进行了多少次猜测。注意:如果这有帮助,请不要忘记what to do when someone answers my question on stackoverflow
    【解决方案2】:

    您正在使用一些冗余检查。首先,continue/break 语句用于在循环中打破循环或传递到下一次迭代,因此在 if/else 中使用它们不会有任何好处。其次,你永远不会增加你的 guess 变量,所以它永远不会跳出循环。您需要在每次迭代中增加它。我想出的最终代码是:

    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')
    

    【讨论】:

      猜你喜欢
      • 2016-06-21
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多