简体   繁体   English

Python Guess游戏如何重复

[英]Python Guess game how to repeat

I have been working on this guessing game but i just cant get it to repeat the game when the player says yes. 我一直在研究这个猜谜游戏,但是当玩家说是的时候,我无法让它重复游戏。 The game gives you 5 attempts to guess the number that it thought of and then after it asks you if you would like to play again but when you say 'YES' it just keeps repeating the sentence and when you say 'NO' it does what its supposed to which is break the code 游戏给您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()

It's because your game code isn't in the function. 这是因为您的游戏代码不在功能中。 Try it in this manner: 以这种方式尝试:

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

There's a few problems with your code that I'd like to point out. 我想指出您的代码有一些问题。 The main one being that your game does not run again when typing yes. 主要的原因是,键入“是”时,您的游戏不会再次运行。 All it will do is run main() which will print your game and then ask you if you want to retry once again. 它所要做的就是运行main() ,它将打印your game ,然后询问您是否要重试。 It's easier if you put your game inside a definition that way you can call it whenever necessary. 如果将游戏放入定义中会更容易,以便可以在需要时调用它。

Also, I don't know if it's just me, but if you guess the correct number, it will still ask you to guess a number. 另外,我不知道是否只有我一个人,但是如果您猜对了数字,它仍然会要求您猜测一个数字。 You need to exit your loop by putting your play_again() method in your else block. 您需要通过将play_again()方法放在else块中退出循环。

Below is the code. 下面是代码。 I've polished it up a little just for optimization. 我已经对其进行了优化,仅用于优化。

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

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

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