根据指令重写

时间:2018-10-09 01:14:50

标签: python url-rewriting instructions

该程序有效,但不遵循要求的布局。有时我很难理解说明,所以我通常会按照自己的方式去做。我可以得到一些帮助,将我所拥有的转换为这些说明吗?可能会帮助我理解所问的内容。预先谢谢你!

1)编写一个函数,使用户输入一个介于1到100之间的猜测值。该方法应使用循环来验证用户输入,并应返回他们输入的值。提示:您已经将此内容作为必需的公共练习之一。

2)编写类似的方法,让用户输入“ y”或“ n”。该方法应该返回他们输入的值,但是应该使用循环来要求重新输入,直到指定了这两个值之一为止。

3)编写一种可以实际玩游戏的方法。这包括选择隐藏的号码和用户猜测的循环。

编码程序的主要部分应类似于:

again = 'y'
while again == 'y':
    call function to play the game once
    again = call function to get "y" or "n" from user

-

import random



def gen_number():

    """
    Generates a random number and returns it.

    :return: Number generated by the computer
    """

    random_number = random.randint(1, 2)
    return random_number


def play_game(question, random_number):

    """
    Accepts user input and checks to see if if input is within the accepted boundaries.
    Calls function replay_game.

    :param question: stores  "Guess a number" in variable question
    :param random_number: Stores the random number generated from gen_random function
    :return: n/a
    """

    correct = None

    while correct != random_number:

        try:
            correct = int(input(question))

        except ValueError:
            print("Please only use integers")
            continue

        if not 1 <= correct <= 100:
            print("Please only enter numbers between 1 and 100!")

        elif correct > random_number:
            print("Too high, try again. ")

        elif correct < random_number:
            print("Too low, try again! ")

        elif correct == random_number:
            print("Yahoo!  You guessed the correct number!")
            replay_game()


def user_input():

    """
    Starts the game by calling function play_game.  Passes arguments "Guess a number & random_number".
    :return: n/a
    """

    print("Welcome to the guessing game!")

    random_number = gen_number()
    play_game("Guess a number: ", random_number)


def replay_game():

    """
    Generates loop to replay game if y or n is entered.  Gives error message if conditions are not met.
    :return:n/a
    """

    replay_yn = ""

    while replay_yn != 'y' or replay_yn != 'n':

        replay_yn = input("Would you like to play again (y/n)? ").lower()

        if replay_yn == "y":
            main()

        elif replay_yn == "n":
            print("See ya later!")
            exit()

        else:
            print("Oops, try again")
            replay_yn = input("y or n")


def main():

    """
    Calls functions user_inout to start the game and replay_game to give options to end or replay.

    :return: n/a
    """

    user_input()
    replay_game()


main()

0 个答案:

没有答案