Python programming basic record a homework exercise (program using programming implementation)

Demand:
#7. Write the following procedure
"""
a. users input 1-7 numbers, representing Monday to Sunday respectively
 b. If you input 1-5, print the corresponding "Monday" to "Sunday". If you input 6 or 7, print out "weekend"
c. If you enter 0, exit the cycle
 d. input other contents and prompt: "wrong input, please input again!"
Tip: if and while loops can be used for this question, and it is necessary to verify whether the user's input is correct
"""

Idea: check whether the user input is correct, but check the control character only once; input number returns 1-7 output corresponding week, 0 exit cycle ends, greater than 7 is equivalent to input other content, will prompt input error, please input again! And it is a while loop to judge isdigit() until the input 1-7 numeric characters are correct, and then convert the numeric characters to int type. Loop user input 1-7.

def digit_game(Num_1):
    """
    a.User input 1-7 Seven numbers, Monday to Sunday
    b.If you enter 1~5,Print the corresponding "Monday"~"Sunday ", if the number entered is 6 or 7, print out" weekend "
    c.If you enter 0, exit the loop
    d.Input other content, prompt: "input error, please re-enter!"
    //Tip: if and while loops can be used for this question, and it is necessary to verify whether the user's input is correct
    """
    # Define a list, immutable type, list all weeks
    list_3 = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Weekend", "Weekend")

    # Cycle judgment to verify that the user input is correct
    while Num_1.isdigit() != True:
        Num_1 = input("1 Input error, please input 1-7 Any number of ranges:")
        while  int(Num_1) > 7:
            print("2 The value entered is{},Not in 1-7 Between.".format(Num_1))
            Num_1 = input("3 Please re-enter 1-7 Any number of ranges:")
            while Num_1.isdigit() != True:
                Num_1 = input("4 Input error, please input 1-7 Any number of ranges:")

    # Cast to int
    a = int(Num_1)
    
    while 1:
        if a == 1 or a == 2 or a == 3 or a == 4 or a == 5 or a == 6 or a == 7:
            print("Today{}".format(list_3[a - 1]))
            if a <= 7:
                Num_1 = input("5 Please input 1-7 Any number of ranges:")
                while Num_1.isdigit() != True:
                    Num_1 = input("6 Input error, please input 1-7 Any number of ranges:")
                a = int(Num_1)
        elif a == 0:
            print("0 Quit the game!")
            break
        else:
            Num_1 = input("7 Input error, please input 1-7 Any number of ranges:")
            while Num_1.isdigit() != True:
                Num_1 = input("8 Input error, please input 1-7 Any number of ranges:")
            a = int(Num_1)
            continue

# Game portal
Num_1 = input("Play a game of entering a number corresponding to the day of the week!\n Please input 1-7 Any number of ranges:")
# Call method, passing in parameters
digit_game(Num_1)

 

Posted by hex on Mon, 09 Dec 2019 02:43:30 -0800