我是编码初学者,我想创建一个如下所示的函数:a = [1, 2, 3, 4, 5, 6]n = int(input("What is your age = "))def prize(a): for n in range(20, 61): if 20 < n <= 30: print('If you are between 20 and 30 years old, you will receive only', a[1], 'prize')截至目前,即使我输入 20 到 30 之间的数字,我的代码也不会打印任何内容。我知道我必须使用 return 语句才能实际运行,但我想知道有没有办法我可以在函数内编写多个 if 语句吗?他们真的能够跑步吗?
1 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
您忘记调用该函数。
是的,您可以if根据需要编写任意数量的语句:
a = [1, 2, 3, 4, 5, 6]
n = int(input("What is your age = "))
def prize(a):
for n in range(20, 61):
if n == 30:
print('You are 30 years old')
elif n == 40:
print('You are 40 years old')
elif 20 < n <= 30:
print('If you are between 20 and 30 years old, you will receive only', a[1], 'prize')
prize(a)
添加回答
举报
0/150
提交
取消