简体   繁体   English

为什么我的输入语句不接受程序中的值?

[英]Why won't my input statements accept values in my program?

I am making a program that generates a random number and asks you to guess the number out of the range 1-100.我正在制作一个生成随机数的程序,并要求您猜测 1-100 范围之外的数字。 Once you put in a number, it will generate a response based on the number.输入数字后,它会根据该数字生成响应。 In this case, it is Too high , Too low , Correct , or Quit too soon if the input is 0 , which ends the program(simplified, but basically the same thing).在这种情况下,如果输入为0 ,则为Too highToo lowCorrectQuit too soon ,程序结束(简化,但基本相同)。

It counts the number of attempts based on how many times you had to do the input function, and it uses a while loop to keep asking for the number until you get it correct.它根据您必须输入 function 的次数来计算尝试次数,并使用 while 循环不断询问数字,直到您输入正确为止。 (btw, yes I realize this part is a copy of my other question. This is a different problem in the same program, so I started it the same way.) (顺便说一句,是的,我意识到这部分是我另一个问题的副本。这是同一程序中的不同问题,所以我以同样的方式开始。)

Anyways, I am having an issue with the last part of the program not taking any values.无论如何,我对程序的最后一部分没有采取任何价值有疑问。 It is supposed to take the input for keep_playing and continue going if it is equal to 'y' .它应该接受keep_playing的输入,如果它等于'y'则继续播放。 The issue is that it isn't actually making the variable equal anything(at least I don't think so.) So, whatever value I put in, it just prints the same response every time.问题是它实际上并没有使变量等于任何东西(至少我不这么认为。)所以,无论我输入什么值,它每次都会打印相同的响应。 Here is the small part of the code which isn't working, though I feel like it is something wrong with the rest of the code:这是代码的一小部分不起作用,尽管我觉得代码的 rest 有问题:

def keep_playing(attempts,keep_playing):
    keep_playing = 'y'
    if keep_playing == 'y':
        guess(attempts)
        keep_playing = str(input("Another game (y to continue)? "))
    else:
        print()
        print("Thanks for playing")
    return keep_playing

The expected output is:预期的 output 是:

Enter a number between 1 and 100, or 0 to quit: 4
Too low, try again       It's 66 for testing purposes
Enter a number between 1 and 100, or 0 to quit: 67
Too high, try again      It's 66 for testing purposes
Enter a number between 1 and 100, or 0 to quit: 66

Congratulations! You guessed the right number!
There were 2 attempts

Another game (y to continue)? y

Enter a number between 1 and 100, or 0 to quit: 0

You quit too early
The number was  79
Another game (y to continue)? n

Thanks for playing!

But the actual output is:但实际的 output 是:

Enter a number between 1 and 100, or 0 to quit: 4
Too low, try again       It's 66 for testing purposes
Enter a number between 1 and 100, or 0 to quit: 67
Too high, try again      It's 66 for testing purposes
Enter a number between 1 and 100, or 0 to quit: 66

Congratulations! You guessed the right number!
There were 2 attempts

Another game (y to continue)? y

Enter a number between 1 and 100, or 0 to quit: 0

You quit too early
The number was  79
Another game (y to continue)? n
Another game (y to continue)? y
>>>

Notice how no matter what I do, it continues to run.请注意,无论我做什么,它都会继续运行。 The first part with the higher and lower works fine, however the bottom part just seems to break, and I don't know how to fix it.较高和较低的第一部分工作正常,但底部似乎坏了,我不知道如何修复它。 If anyone has any solutions that would be greatly appreciated.如果有人有任何解决方案,将不胜感激。

Also, in case anyone wanted to see the whole thing, in case there was in issue with that, here it is:另外,如果有人想看到整个事情,如果有问题的话,这里是:

import random

def main():
    global attempts
    attempts = 0
    guess(attempts)
    keep_playing(attempts,keep_playing)
    
def guess(attempts):
    number = random.randint(1,100)
    print('')
    guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
    while guess != 0: 
        if guess != number:
            if guess < number:
                print("Too low, try again       It's",number, "for testing purposes") #printing the number makes it easier to fix :/
                attempts += 1
                guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
            elif guess > number:
                print("Too high, try again      It's",number, "for testing purposes")
                attempts += 1
                guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
        else:
            print()
            print("Congratulations! You guessed the right number!")
            print("There were", attempts,"attempts")
            print()
            keep_playing = str(input("Another game (y to continue)? "))
            return keep_playing
            
    else:
        print()
        print("You quit too early")
        print("The number was ",number)
        keep_playing = str(input("Another game (y to continue)? "))
        return keep_playing
        
    
    

def keep_playing(attempts,keep_playing):
    keep_playing = 'y'
    if keep_playing == 'y':
        guess(attempts)
        keep_playing = str(input("Another game (y to continue)? "))
    else:
        print()
        print("Thanks for playing")
    return keep_playing
main()
            

I notice a couple things here我注意到这里有几件事

  1. There is some issue with the naming of your function, python thinks that keep_playing is the str variable keep_playing and not the function. In my code below I will rename the function keep_playing to keep_playing_game .您的 function 的命名存在一些问题,python 认为keep_playing是 str 变量keep_playing而不是 function。在我下面的代码中,我将 function keep_playing重命名为keep_playing_game
  2. You need to pass in the parameters when you call the function keep_playing_game so the function knows what the user input and attempts are.您需要在调用 function keep_playing_game时传入参数,以便 function 知道用户输入和尝试的内容。
  3. Why are you setting keep_playing = 'y' in the first line of your function def keep_playing_game(attempts,keep_playing) ?为什么要在 function def keep_playing_game(attempts,keep_playing)的第一行中设置keep_playing = 'y' If you remove this line, your program should run as expected based on the value the user enters and not what the function assigns keep_playing to.如果删除此行,您的程序应根据用户输入的值而不是 function 分配给keep_playing的值按预期运行。

I would recommend trying something like this我建议尝试这样的事情

import random

def main():
    global attempts
    attempts = 0
    guess(attempts)
    # keep_playing(attempts,keep_playing) -> this line should be removed
    
def guess(attempts):
    number = random.randint(1,100)
    print('')
    guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
    while guess != 0: 
        if guess != number:
            if guess < number:
                print("Too low, try again       It's",number, "for testing purposes") #printing the number makes it easier to fix :/
                attempts += 1
                guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
            elif guess > number:
                print("Too high, try again      It's",number, "for testing purposes")
                attempts += 1
                guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
        else:
            print()
            print("Congratulations! You guessed the right number!")
            print("There were", attempts,"attempts")
            print()
            keep_playing = str(input("Another game (y to continue)? "))
            return keep_playing_game(keep_playing, attempts)
            
    else:
        print()
        print("You quit too early")
        print("The number was ",number)
        keep_playing = str(input("Another game (y to continue)? "))
        return keep_playing_game(keep_playing, attempts)
        
    
    

def keep_playing_game(keep_playing, attempts):
    if keep_playing == 'y':
        guess(attempts)
    else:
        print()
        print("Thanks for playing")
        return
    return None
main()

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

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