繁体   English   中英

猜数字游戏(Python)

[英]Number Guessing Game (Python)

工作在 Python 3.

我对 Python 还是比较陌生(只有几周的知识)。

该程序给我的提示是编写一个随机数游戏,用户必须猜测随机数(在 1 到 100 之间),如果不正确,则会给出太低或太高的提示。 然后用户会一次又一次地猜测,直到他们找到解决方案。 解决方案后,猜测的数量应该在最后统计。

import random

def main():

    # initialization
    high = 0
    low = 0
    win = 0
    number = random.randint(1, 100)

    # input
    userNum = int(input("Please guess a number between 1 and 100: "))

    # if/else check
    if userNum > number:
        message = "Too high, try again."
        high += 1
    elif userNum == number:
        message = "You got it correct! Congratulations!"
        win += 1
    else:
        message = "Too low, try again."
        low += 1
    print()
    print(message)

    # loop
   # while message != "You got it correct! Congratulations!":

    # display total
    print()
    print("Number of times too high: ", high)
    print("Number of times too low: ", low)
    print("Total number of guesses: ", (high + low + win))     

main()

我正在努力弄清楚如何使循环工作。 我需要随机数为 static 而用户猜测输入。 每次尝试后,我还需要他们收到来自 if/else 检查的正确消息提示。

您可以将 'userNum' 设置为 0,并将所有输入包含在一个 while 循环中:

userNum = 0

while (userNum != number):

这将不断循环猜谜游戏,直到用户的猜测等于随机数。 这是完整的代码:

import random

def main():

    # initialization
    high = 0
    low = 0
    win = 0
    number = random.randint(1, 100)
    userNum = 0

    while (userNum != number):
        # input
        userNum = int(input("Please guess a number between 1 and 100: "))

        # if/else check
        if userNum > number:
            message = "Too high, try again."
            high += 1
        elif userNum == number:


        message = "You got it correct! Congratulations!"
        win += 1
        else:
            message = "Too low, try again."
            low += 1
    print()
    print(message)

    # loop
   # while message != "You got it correct! Congratulations!":

    # display total
    print()
    print("Number of times too high: ", high)
    print("Number of times too low: ", low)
    print("Total number of guesses: ", (high + low + win))


    print("You win")

main()

你可以把你的猜测放在一个简单的循环中:

import random

def main():

    # initialization
    high = 0
    low = 0
    win = 0
    number = random.randint(1, 100)

    while win == 0:
        # input
        userNum = int(input("Please guess a number between 1 and 100: "))

        # if/else check
        if userNum > number:
            message = "Too high, try again."
            high += 1
        elif userNum == number:
            message = "You got it correct! Congratulations!"
            win += 1
        else:
            message = "Too low, try again."
            low += 1
        print()
        print(message)

    # loop
   # while message != "You got it correct! Congratulations!":

    # display total
    print()
    print("Number of times too high: ", high)
    print("Number of times too low: ", low)
    print("Total number of guesses: ", (high + low + win))

main()

任何其他答案都可以使用,但您可以为循环执行的另一项检查是“不赢”,这将停止循环以获取任何不为零的值。

 while not win:
    # input
    userNum = int(input("Please guess a number between 1 and 100: "))

    # if/else check
    ...
import random

def main():

# initialization
high = 0
low = 0
win = 0
number = random.randint(1, 100)

# input

while(True): #Infinite loop until the user guess the number.
    userNum = int(input("Please guess a number between 1 and 100: "))
# if/else check
    if userNum > number:
        message = "Too high, try again."
        high += 1

    elif userNum == number:
        message = "You got it correct! Congratulations!"
        win += 1
        break #Break out of the infinite loop 

    else:
        message = "Too low, try again."
        low += 1
    print(message) ##Display the expected message

print()
print(message)



# display total
print()
print("Number of times too high: ", high)
print("Number of times too low: ", low)
print("Total number of guesses: ", (high + low + win))     


if __name__ == '__main__':
    main()

有很多方法可以完成此类任务,这就是使编程成为史诗的原因。

我写了一些东西来回答你的问题,它甚至有一种简单的感觉!

我所做的是一个 for 循环,为用户提供总共 range(10) 次尝试。 对于用户所做的每个猜测,它都会将 1 次“尝试”加到 0。在我的 if 语句中,我只想知道猜测是否与 the_number 相同并且低于可用的尝试次数,您已经完成了. 否则,更高或更低:)

希望这可以帮助!

import random


print("\nWelcome to the guessing game 2.0")
print("Example for Stack Overflow!\n")

the_number = random.randint(1, 100)
tries = 0

for tries in range(10):
    guess = int(input("Guess a number: "))
    tries += 1

    if guess == the_number and tries <= 10:
        print("\nCongratulations! You've guessed it in", tries, "tries!")
        break
    elif guess < the_number and tries < 10:
        print("Higher...")
        tries += 1
    elif guess > the_number and tries < 10:
        print("Lower...")
        tries += 1
    elif tries >= 11:
        print("\nI'm afraid to haven't got any tries left. You've exceeded the limit.")
import random
def display_title():
    return print("Number Guessing Game")
def play_game():
    cpu_num = random.randint(1, 10)
    user_guess = int(input("Guess a number from 1 to 10:")
    while user_guess != cpu_num:
        print("Try Again")
        user_guess = int(input("Guess a number from 1 to 10:"))
    if user_guess == cpu_num:
        print("Correct!")
def main():
    title = print(display_title())
    game = print(play_game())

    return title, game
print(main())

暂无
暂无

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

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