简体   繁体   English

如何为 1-100 之间的猜数字游戏创建用户输入循环?

[英]How can I create a user input loop for a Guess the Number game between 1-100?

#Guess the number

from random import *
correct_number = randint(1, 100)
#print(correct_number)

def guess(user_guess):
    while user_input != correct_number:
        if user_guess == correct_number:
            return f"{correct_number} is the correct number! Congratulations! Would you like to play again?"
        elif user_guess > correct_number:
            return "Guess a lower number\n"
            user_input = int(user_input("Guess a number between 1 and 100.\n"))
        else:
            return "Guess a higher number\n"
            user_input = int(user_input("Guess a number between 1 and 100.\n"))

"""user_input = ""
while user_input != correct_number:
    user_input = input("Hey user, guess a number between 1 and 100.\n")"""

user_input = input("Hey user, guess a number between 1 and 100.\n")
user_input_guess = int(user_input)

value = guess(user_input_guess)
print(value)

I have the bulk of the code ready.我已经准备好了大部分代码。 I just can't nail the user input loop.我只是无法确定用户输入循环。 Each time I guess a number it exits the code.每次我猜一个数字时,它都会退出代码。

from random import randint

correct_number = randint(1, 100)

user_input = ""
while user_input != correct_number:
    user_input = int(input("Hey user, guess a number between 1 and 100.\n"))

No need to use functions.无需使用函数。 You forgot to convert the input to an int in the code inside the comment so that's why it wasn't working您忘记在评论中的代码中将输入转换为 int,所以这就是它不起作用的原因

return will break the define so you should return the data when the user guess the correct answer return将打破定义,因此当用户猜出正确答案时,您应该返回数据

from random import *
correct_number = randint(1, 100)
#print(correct_number)

def guess(user_guess):
    while user_guess != correct_number:
        if user_guess == correct_number:
            return f"{correct_number} is the correct number! Congratulations! Would you like to play again?"
        elif user_guess > correct_number:
            print("Guess a lower number\n")
            user_guess = int(input("Guess a number between 1 and 100.\n"))
        else:
            print("Guess a higher number\n")
            user_guess = int(input("Guess a number between 1 and 100.\n"))

"""user_input = ""
while user_input != correct_number:
    user_input = input("Hey user, guess a number between 1 and 100.\n")"""

user_input = input("Hey user, guess a number between 1 and 100.\n")
user_input_guess = int(user_input)

value = guess(user_input_guess)
print(value)

This will work这将工作

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

相关问题 如何在 Python 中将用户输入限制为仅某些整数(即:仅在 1-100 之间,其中还包括 1 和 100) - How can I limit the user input to only certain integers (ie: Only between 1-100, which also includes 1 and 100) in Python 猜数字游戏:我如何接受数字前面输入“ guess =”的猜测? - Number guessing game: How can I accept input that says “guess =” before the number? 如何猜测0-100:50之间的数字? - How to Guess a number between 0-100:50? 如何在 Python 中修复我的猜数字游戏? - How can I fix my Guess The Number game in Python? 我正在尝试创建一个程序,其中计算机在 1 到 100 之间选择一个随机数并尝试猜测它选择的数字 - I am trying to create a program in which the computer picks a random number between 1 and 100 and tries to guess the number it picked 创建一个介于1到100之间且不会每次更改的随机数 - Creating a random number between 1-100 that doesn't change everytime 如何让我的随机数猜谜游戏在用户输入时再次循环以及如何创建错误陷阱? - How to get my random number guessing game to loop again upon user input and how to create an error trap? 如何重新开始猜数字游戏 - How to restart a guess number game 如何制作猜数字游戏? - how to make a guess number game? 猜数字游戏 python 我似乎无法让它工作 - guess the number game python I can't seem to make it work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM