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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将代码更改为函数并在它们之间传递变量?相关的知识,希望对你有一定的参考价值。

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

我曾尝试将它们放入函数中,但随后我的变量出现错误。继承人的代码:

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

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

答案

请参见下面的代码,以获取更多说明。

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()
另一答案
import random

def choose_level():
    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 Multiplacation, 4 for Division, and then press the 'Enter' key. "))

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

def solve(level, randoms):
    rounds = 0
    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")
    print("Thanks for playing")

def main():
    level = choose_level()
    randoms = set_randoms()
    solve(level, randoms)

if __name__ == "__main__":
    main()

以上是关于将代码更改为函数并在它们之间传递变量?的主要内容,如果未能解决你的问题,请参考以下文章

如何将活动 UI 的点击传递到地图片段以将地图更改为 MAP_TYPE_HYBRID

将局部变量更改为全局变量不起作用

Android:将“ViewPager”动画从片段更改为片段

如何在导航抽屉活动模板中的片段之间传递字符串变量

如何在 React JS 中打开一个新页面并在它们之间传递数据

如何将我的代码从回调函数更改为承诺 [重复]