首页 > 解决方案 > 将代码更改为函数并在它们之间传递变量?

问题描述

我有一个数学测验程序。我被告知将我的代码移动到函数中,然后让这些函数由主列表调用。你是如何创建函数的?

我试过把它们放在函数中,但是我的变量有错误。继承人的代码:

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

如何让我的代码仍然可以工作,但也可以使用函数。

标签: python

解决方案


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

推荐阅读