猜数字游戏(Python)

时间:2018-10-29 02:49:53

标签: 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()

我正在努力弄清楚如何使循环起作用。当用户用输入猜测时,我需要随机数是静态的。每次尝试之后,我还需要使用if / else检查中的正确消息来提示他们。

5 个答案:

答案 0 :(得分:0)

您可以将'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()

答案 1 :(得分:0)

您可以将您的猜测放在一个简单的循环中:

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

答案 2 :(得分:0)

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

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

    # if/else check
    ...

答案 3 :(得分:0)

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

答案 4 :(得分:0)

执行此类任务的方法有很多,这就是使编程变得史诗般的原因。

我写了一些可以回答您问题的东西,甚至感觉很简单!

我所做的是完成一个for循环,使用户可以进行range(10)次尝试。用户进行的每个猜测,都会在0后面加上1个“ tries”。在我的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.")