将代码更改为函数并在它们之间传递变量?

时间:2019-10-20 01:00:23

标签: python

我有一个数学测验程序。有人告诉我将代码移入函数,然后由主列表调用这些函数。您如何创建函数?

我尝试将它们放入函数中,但是随后我的变量出现错误。 以下是该代码:

import random

def welcome():
    level = 0
    rounds = 0

    print("Welcome to the math quiz.  To get started you will need to select a level.")
level = int(input("Press 1 for Addition, 2 for Subtraction, 3 for Multiplacation, 4 for Division, and then press the 'Enter' key. "))

def random():
    number_one = random.randrange(1,10)
    number_two = random.randrange(1,10)

def levels():
    if level == 1:
        solution = number_one + number_two
        print("What is", number_one, "plus", number_two, "?")
        user_ans = int(input())
    elif level == 2:
        solution = number_one - number_two
        print("What is", number_one, "minus", number_two, "?")
        user_ans = int(input())
    elif level == 3:
        solution = number_one * number_two
        print("What is", number_one, "times by", number_two, "?")
        user_ans = int(input())
    elif level == 4:
        solution = number_one / number_two
        print("What is", number_one, "divided by", number_two, "?")
        user_ans = int(input())

def checker():
    if user_ans == solution:
        print("Correct")
        number_one = random.randrange(1,10)
        number_two = random.randrange(1,10)
        rounds = rounds + 1
    else:
        print("Try again")


welcome()
random()
while rounds < 10:
    levels()
    checker()
print("Thanks for playing")

不用担心处理错误的数字/字符或拼写错误的所有问题,我需要修复一些东西以显示对老师的改进。

这是原始的工作代码:

import random

level = 0
rounds = 0

print("Welcome to the math quiz.  To get started you will need to select a level.")
level = int(input("Press 1 for Addition, 2 for Subtraction, 3 for Multiplacation, 4 for Division, and then press the 'Enter' key. "))

number_one = random.randrange(1,10)
number_two = random.randrange(1,10)

while rounds < 10:
    if level == 1:
        solution = number_one + number_two
        print("What is", number_one, "plus", number_two, "?")
        user_ans = int(input())
    elif level == 2:
        solution = number_one - number_two
        print("What is", number_one, "minus", number_two, "?")
        user_ans = int(input())
    elif level == 3:
        solution = number_one * number_two
        print("What is", number_one, "times by", number_two, "?")
        user_ans = int(input())
    elif level == 4:
        solution = number_one / number_two
        print("What is", number_one, "divided by", number_two, "?")
        user_ans = int(input())

    if user_ans == solution:
        print("Correct")
        number_one = random.randrange(1,10)
        number_two = random.randrange(1,10)
        rounds = rounds + 1
    else:
        print("Try again")

print("Thanks for playing")

如何使我的代码仍然可以正常使用,还可以使用函数。

2 个答案:

答案 0 :(得分:1)

import random

def choose_level():
    ''' Returns the level as an integer '''
    print("Welcome to the math quiz.  To get started you will need to select a level.")
    return int(input("Press 1 for Addition, 2 for Subtraction, 3 for Multiplication, 4 for Division, and then press the 'Enter' key. "))

def set_randoms():
    ''' Returns the numbers as a tuple '''
    number_one = random.randrange(1,10)
    number_two = random.randrange(1,10)
    return (number_one, number_two)

def solve(level, randoms):
    rounds = 0
    # sets your number_one and number_two to the input tuple
    number_one, number_two = randoms
    while rounds < 10:
        if level == 1:
            solution = number_one + number_two
            print("What is", number_one, "plus", number_two, "?")
            user_ans = int(input())
        elif level == 2:
            solution = number_one - number_two
            print("What is", number_one, "minus", number_two, "?")
            user_ans = int(input())
        elif level == 3:
            solution = number_one * number_two
            print("What is", number_one, "times by", number_two, "?")
            user_ans = int(input())
        elif level == 4:
            solution = number_one / number_two
            print("What is", number_one, "divided by", number_two, "?")
            user_ans = int(input())
        if user_ans == solution:
            print("Correct")
            number_one = random.randrange(1,10)
            number_two = random.randrange(1,10)
            rounds = rounds + 1
        else:
            print("Try again")
    # when done solving send your thanks message
    print("Thanks for playing")

def main():
    # first get your level and set it to the function
    # this will make the "return" be set as level
    level = choose_level()
    # do the same for randoms
    randoms = set_randoms()
    # start your solve but send level and randoms as inputs to be used
    # in the function
    solve(level, randoms)

if __name__ == "__main__":   # this is common for python files and means
    main()                   # to run the main() function if this file is not
                             # an import (ie this file is being ran directly
                             # as the __main__ file

答案 1 :(得分:0)

有关更多说明,请参见下面的代码。

import random

def welcome():
    print("Welcome to the math quiz.  To get started you will need to select a level.")
    level = int(input("Press 1 for Addition, 2 for Subtraction, 3 for Multiplacation, 4 for Division, and then press the 'Enter' key. "))
    return level # Send level back to main function.

def random_numbers():
    return (random.randrange(1,10), random.randrange(1,10)) # Return the two numbers in a tuple.

def next_round(number_one, number_two, rounds):
    if level == 1:
        solution = number_one + number_two
        print("What is", number_one, "plus", number_two, "?")
        user_ans = int(input())
    elif level == 2:
        solution = number_one - number_two
        print("What is", number_one, "minus", number_two, "?")
        user_ans = int(input())
    elif level == 3:
        solution = number_one * number_two
        print("What is", number_one, "times by", number_two, "?")
        user_ans = int(input())
    elif level == 4:
        solution = number_one / number_two
        print("What is", number_one, "divided by", number_two, "?")
        user_ans = int(input())

    if user_ans == solution:
        print("Correct")
        number_one = random.randrange(1,10)
        number_two = random.randrange(1,10)
        rounds = rounds + 1
    else:
        print("Try again")
    return rounds

def goodbye():
    print("Thanks for playing")

if __name__=="__main__": # Main function, runs only when program is run from terminal.
    level = welcome()
    rounds = 0
    while rounds < 10:
        rounds = next_round(*random_numbers(), rounds) # Unpack random numbers and send to next_round
    goodbye()