如何通过简单的是或否答案重复或结束一个功能?

时间:2016-10-30 20:37:19

标签: python

我想创建一个猜谜游戏以获得更舒适的编程,用户最多可以进行100次猜测(绰绰有余)。如果数字太高或太低,它们会输入一个新的输入,如果它正确,它将打印正确。现在我只想将它设置到我问他们想要再次播放的地方。我想我有一个想法,通过将它们分成两个函数来设置它? 我知道这不是一个功能,但应该把它作为一个功能,然后把我的问题作为一个if语句在自己的函数中?

import random


randNum = random.randrange(1,21)
numguesses = 0

while numguesses < 100:
        numguesses = numguesses + 1
        userguess = int(input("What is your guess [1 through 20]?"))
        if userguess < 1:
            print("Too Low")
            print("Please enter a valid guess [1-20]!")  
        elif userguess > 20:
                print("Too High")
        elif userguess == randNum:
            print("Correct")
            print("you used",numguesses,"number of guesses")

4 个答案:

答案 0 :(得分:1)

这是一个简单的方法,就像你问的那样。我做了一个功能,当你得到正确的东西时,它询问你是否想要再次玩,如果你输入“是”,那么它会重置vars并再次运行循环。如果你输入的内容只有“是”,那么就会打破结束程序的循环。

import random

def main():
    randNum = random.randrange(1,21)
    numguesses = 0

    while numguesses < 100:
            numguesses = numguesses + 1
            userguess = int(input("What is your guess [1 through 20]?"))
            if userguess < 1:
                print("Too Low")
                print("Please enter a valid guess [1-20]!")
            elif userguess > 20:
                    print("Too High")
            elif userguess == randNum:
                print("Correct")
                print("you used",numguesses,"number of guesses")
                x = input("would you like to play again?")
                if x == "yes":
                    main()
                else:
                    break

main()

答案 1 :(得分:0)

这是另一种方法

import random

randNum = random.randrange(1,21)
numguesses = 0
maxGuess = 100

print("Guessing number Game - max attempts: " + str(maxGuess))
while True:
    numguesses +=1
    userguess = int(input("What is your guess [1 through 20]? "))
    if userguess < randNum:
        print("Too Low")
    elif userguess > randNum:
        print("Too High")
    else:
        print("Correct. You used ",numguesses," number of guesses")
        break    
    if maxGuess==numguesses:
        print("Maximum attempts reached. Correct answer: " + str(randNum))
        break

答案 2 :(得分:0)

import random


randNum = random.randrange(1, 21)
guess = 0

response = ['too low', 'invalid guess', 'too hight', 'correct']


def respond(guess):
    do_break = None # is assigned True if user gets correct answer

    if guess < randNum:
        print(response[0])

    elif guess > randNum:
        print(response[2])

    elif guess < 1:
        print(response[1])

    elif guess == randNum:
        print(response[3])

        do_continue = input('do you want to continue? yes or no')

        if do_continue == 'yes':
            # if player wants to play again start loop again
            Guess()
        else:
            # if player does'nt want to play end game
            do_break = True # tells program to break the loop

    # same as ''if do_break == True''
    if do_break:
        #returns instructions for loop to end
        return True


def Guess(guess=guess):
    # while loops only have accesse to variables of direct parent
    # which is why i directly assigned the guess variable to the Fucntion
    while guess < 100:
        guess -= 1

        user_guess = int(input('What is your guess [1 through 20]?'))

        # here the respond function is called then checked for a return
        # statement (note i don't know wheter this is good practice or not)
        if respond(user_guess):
            # gets instructions from respond function to end loop then ends it
            break

Guess()

答案 3 :(得分:-1)

使用两个while循环的另一种方式

answer = 'yes'
while answer == 'yes':
    while numguesses < 100:
        numguesses = numguesses + 1
        userguess = int(input("What is your guess [1 through 20]?"))
        if userguess < 1:
            print("Too Low")
            print("Please enter a valid guess [1-20]!")
        elif userguess > 20:
            print("Too High")
        elif userguess == randNum:
            print("Correct")
            print("you used",numguesses,"number of guesses")
            break  #Stop while loop if user guest, hop to the first loop with answer var
    answer = raw_input("Would you like to continue? yes or no\n>")