Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将代码更改为函数并在它们之间传递变量?_Python - Fatal编程技术网

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

Python 将代码更改为函数并在它们之间传递变量?,python,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

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

我试着把它们放在函数中,但是我的变量有错误。 下面是代码:

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")
随机导入
def welcome():
级别=0
轮数=0
打印(“欢迎参加数学测验。要开始,您需要选择一个级别。”)
级别=整数(输入(“按1表示加法,按2表示减法,按3表示乘法,按4表示除法,然后按“回车”键)。)
def random():
number_-one=随机。随机范围(1,10)
number_two=random.randrange(1,10)
def级别():
如果级别==1:
解决方案=数字1+数字2
打印(“What is”,number\u one,“plus”,number\u two,”?)
用户_ans=int(输入())
elif级别==2:
解决方案=数字1-数字2
打印(“什么是”、数字1、“减”、数字2“?”)
用户_ans=int(输入())
elif级别==3:
解决方案=第1个*第2个
打印(“What is”,number\u one,“times by”,number\u two,”?)
用户_ans=int(输入())
elif级别==4:
解决方案=数字1/数字2
打印(“什么是”,数字1,“除以”,数字2“?”)
用户_ans=int(输入())
def检查器():
如果用户_ans==解决方案:
打印(“正确”)
number_-one=随机。随机范围(1,10)
number_two=random.randrange(1,10)
轮数=轮数+1
其他:
打印(“重试”)
欢迎
随机的
当轮数小于10时:
级别()
检查器()
打印(“谢谢播放”)
不要担心处理错误的数字/字符或拼写的所有问题,我需要解决一些问题,以向我的老师展示改进

以下是原始工作代码:

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")
随机导入
级别=0
轮数=0
打印(“欢迎参加数学测验。要开始,您需要选择一个级别。”)
级别=整数(输入(“按1表示加法,按2表示减法,按3表示乘法,按4表示除法,然后按“回车”键)。)
number_-one=随机。随机范围(1,10)
number_two=random.randrange(1,10)
当轮数小于10时:
如果级别==1:
解决方案=数字1+数字2
打印(“What is”,number\u one,“plus”,number\u two,”?)
用户_ans=int(输入())
elif级别==2:
解决方案=数字1-数字2
打印(“什么是”、数字1、“减”、数字2“?”)
用户_ans=int(输入())
elif级别==3:
解决方案=第1个*第2个
打印(“What is”,number\u one,“times by”,number\u two,”?)
用户_ans=int(输入())
elif级别==4:
解决方案=数字1/数字2
打印(“什么是”,数字1,“除以”,数字2“?”)
用户_ans=int(输入())
如果用户_ans==解决方案:
打印(“正确”)
number_-one=随机。随机范围(1,10)
number_two=random.randrange(1,10)
轮数=轮数+1
其他:
打印(“重试”)
打印(“谢谢播放”)

如何使我的代码既能工作,又能使用函数。

有关详细说明,请参阅下面代码中的注释

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()
随机导入
def welcome():
打印(“欢迎参加数学测验。要开始,您需要选择一个级别。”)
级别=整数(输入(“按1表示加法,按2表示减法,按3表示乘法,按4表示除法,然后按“回车”键)。)
返回级别#将级别发送回主功能。
def随机_数():
return(random.randrange(1,10),random.randrange(1,10))#返回元组中的两个数字。
def下一轮(第一轮、第二轮):
如果级别==1:
解决方案=数字1+数字2
打印(“What is”,number\u one,“plus”,number\u two,”?)
用户_ans=int(输入())
elif级别==2:
解决方案=数字1-数字2
打印(“什么是”、数字1、“减”、数字2“?”)
用户_ans=int(输入())
elif级别==3:
解决方案=第1个*第2个
打印(“What is”,number\u one,“times by”,number\u two,”?)
用户_ans=int(输入())
elif级别==4:
解决方案=数字1/数字2
打印(“什么是”,数字1,“除以”,数字2“?”)
用户_ans=int(输入())
如果用户_ans==解决方案:
打印(“正确”)
number_-one=随机。随机范围(1,10)
number_two=random.randrange(1,10)
轮数=轮数+1
其他:
打印(“重试”)
回程
def再见():
打印(“谢谢播放”)
如果_name _==“_main _”:#main函数仅在程序从终端运行时运行。
级别=欢迎()
轮数=0
当轮数小于10时:
轮数=下一轮(*随机数(),轮数)#解包随机数并发送到下一轮
再见
随机导入
def选择_级别():
''以整数形式返回级别''
打印(“欢迎参加数学测验。要开始,您需要选择一个级别。”)
返回int(输入(“按1表示加法,按2表示减法,按3表示乘法,按4表示除法,然后按Enter键”)
def set\U随机应变()
''以元组形式返回数字''
number_-one=随机。随机范围(1,10)
number_two=random.randrange(1,10)
返回(第一,第二)
def解算(级别,随机):
轮数=0
#将数字1和数字2设置为输入元组
数字1,数字2=随机数
当轮数小于10时:
如果级别==1:
解决方案=数字1+数字2
打印(“What is”,number\u one,“plus”,number\u two,”?)
用户_ans=int(输入())
elif级别==2:
解决方案=数字1-数字2
打印(“什么是”、数字1、“减”、数字2“?”)
用户_ans=int(输入())
elif级别==3:
解决方案=第1个*nu