【问题标题】:Python Guess game how to repeatPython猜猜游戏如何重复
【发布时间】:2017-02-23 05:30:27
【问题描述】:

我一直在研究这个猜谜游戏,但当玩家说是时,我无法让它重复游戏。游戏让您尝试 5 次猜测它想到的数字,然后在询问您是否想再玩一次之后,当您说“是”时,它只会不断重复句子,而当您说“不”时,它会做什么它应该是破坏代码

def main():
    game = "your game"
    print(game)
    play_again()
import random #imports random number function
print("Welcome to the number guessing game!")
counter=1 #This means that the score is set to 0
number = int(random.randint(1,10))
while counter >0 and counter <=5:

    guess=int(input("Try and guess the number\n"))#tells the user to try and guess the number
    if guess!=number and guess>number:
        print("wrong number! Try again you are too high")#tells you that you were wrong and that that you were too high
        counter=counter+1#adds 1 count for every attempt it took you to guess the number

    elif guess!=number and guess<number:
        print("wrong number! Try again you are too low!")#tells you that you were wrong and tells you that you were too low
        counter=counter+1#adds 1 count for every attempt it took you to guess the number
    else:
        print("Well done! You have guessed the number i was thinking of! The number was ",number)#Prints this out when you guessed the number
        print("it took you ",counter, "attempts!")#tells you how many attempts it took you to guess the number

    if counter==2:
        print("4 attempts left before program ends")

    if counter==3:
        print("3 attempts left before program ends")

    if counter==4:
        print("2 attempts left before program ends")

    if counter==5:
        print("1 attempts left before program ends")

def play_again():
    while True:
        play_again = input("Would you like to play again?(yes or no) : ")
        if play_again == "yes":
            main()
        if play_again == "no":
            exit()
        else:
            print("I'm sorry I could not recognize what you entered")
main()

【问题讨论】:

  • 只是想知道...randint() 不是已经属于int 类型了吗?

标签: python helper python-3.5


【解决方案1】:

我想指出您的代码存在一些问题。 主要的原因是您的游戏在输入“是”时不会再次运行。它所要做的就是运行main(),它将打印your game,然后询问您是否要再次重试。如果你把你的游戏放在一个定义中,这样你可以在必要时调用它会更容易。

另外,我不知道是不是只有我一个人,但如果你猜对了,它会仍然要求你猜一个数字。您需要通过将 play_again() 方法放入您的 else 块中来退出循环。

下面是代码。我已经对其进行了一些优化,以进行优化。

import random #imports random number function

def main():
    print("Welcome to the number guessing game!")
    game = "your game"
    print(game)
    run_game()
    play_again()

def run_game():
  counter = 1
  number = random.randint(1, 10)
  while counter > 0 and counter <= 5:
    guess=int(input("Try and guess the number\n"))#tells the user to try and guess the number
    if guess!=number and guess > number:
        print("wrong number! Try again you are too high")#tells you that you were wrong and that that you were too high
        counter=counter+1#adds 1 count for every attempt it took you to guess the number

    elif guess != number and guess < number:
        print("wrong number! Try again you are too low!")#tells you that you were wrong and tells you that you were too low
        counter=counter+1#adds 1 count for every attempt it took you to guess the number
    else:
        print("Well done! You have guessed the number i was thinking of! The number was " + str(number))#Prints this out when you guessed the number
        print("it took you " + str(counter) + " attempts!")#tells you how many attempts it took you to guess the number
        play_again()

    if counter == 2:
        print("4 attempts left before program ends")

    if counter == 3:
        print("3 attempts left before program ends")

    if counter == 4:
        print("2 attempts left before program ends")

    if counter == 5:
        print("1 attempts left before program ends")

def play_again():
    while True:
        retry = input("Would you like to play again?(yes or no) : ")
        if retry == "yes":
            main()
        if retry == "no":
            exit()
        else:
            print("I'm sorry I could not recognize what you entered")
main()

【讨论】:

    【解决方案2】:

    这是因为你的游戏代码不在函数中。以这种方式尝试:

    <import statements>
    
    def game():
        <insert all game code>
    
    def main():
        while True:
            play_again = input("Would you like to play again?(yes or no) : ")
            if play_again == "yes":
                game()
            if play_again == "no":
                exit()
            else:
                print("I'm sorry I could not recognize what you entered")
    

    【讨论】:

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