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")
如何让我的代码仍然可以工作,但也可以使用函数。
解决方案
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
推荐阅读
- java - 在android中制作一个自定义函数,以便我可以在任何地方使用它
- php - 在php中使用mysql的多级动态菜单
- opengl - OpenGL地形网格成高斯斑点
- c# - Unit Testing a method that creates a table with indexes
- python - 无法在 python webscrape 中刮掉所有 UL 标签的文本
- laravel - 如何在下拉列表表单上显示值自动从数据库中选择值
- java - 异步 api 的 CPU 利用率非常高
- ruby - 使用 gem sequel 删除枚举值
- c# - 如何获得一个按钮来切换单独表单上的另一个按钮?
- php - 是否可以将 Apache2 配置为每个虚拟主机使用不同的模块加载 PHP?