猿问

我可以在函数中编写 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 20 < n <= 30:

            print('If you are between 20 and 30 years old, you will receive only', a[1], 'prize')

截至目前,即使我输入 20 到 30 之间的数字,我的代码也不会打印任何内容。我知道我必须使用 return 语句才能实际运行,但我想知道有没有办法我可以在函数内编写多个 if 语句吗?他们真的能够跑步吗?


交互式爱情
浏览 79回答 1
1回答

慕妹3242003

您忘记调用该函数。是的,您可以if根据需要编写任意数量的语句:a = [1, 2, 3, 4, 5, 6]n = int(input("What is your age = "))def prize(a):&nbsp; &nbsp; for n in range(20, 61):&nbsp; &nbsp; &nbsp; &nbsp; if n == 30:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('You are 30 years old')&nbsp; &nbsp; &nbsp; &nbsp; elif n == 40:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('You are 40 years old')&nbsp; &nbsp; &nbsp; &nbsp; elif 20 < n <= 30:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('If you are between 20 and 30 years old, you will receive only', a[1], 'prize')prize(a)
随时随地看视频慕课网APP

相关分类

Python
我要回答