我需要为这个任务创建一个个性任务,我已经写了这段代码.我知道这可能不是最好的方法,但我想知道为什么它不起作用.

我有3个问题,都是函数(这里只显示了1个),它们的 struct 都是一样的,只是我用num_2和num_3代替num_1来回答问题2和3.

def question1():
    print("What's your favorite genre of music?")
    print("1. Pop")
    print("2. Rap")
    print("3. Metal")
    music = int(input("Enter 1, 2, or 3: "))
    if music == 1:
        num_1 = 3
    elif music == 2:
        num_1 = 2
    else:
        num_1 = 1

def main():
    question1()
    if "num_1" + "num_2" + "num_3" == 9:
        print("Your favorite color is red")
    elif 5 < "num_1" + "num_2" + "num_3" < 9:
        print("Your favorite color is blue")
    else:
        print("Your favorite color is green")

main()

推荐答案

代码中有一些错误:

  1. main()函数中,将变量num1num2num3放在if语句的引号中.这是不正确的,因为我们希望比较变量值,而不是变量名.

  2. 即使让函数声明num2num3,因为这些变量(包括num1)是要在其中声明它们的函数的局部变量,main()函数也不会识别它们.

为了解决这个问题,我们可以将num1num2num3定义为global个变量.

考虑到所有这些变化,你的代码可能看起来像这样(当然修改了question2()question3(),所以这是一个真正的个性测验哈哈):

def question1():
    print("What's your favorite genre of music?")
    print("1. Pop")
    print("2. Rap")
    print("3. Metal")
    music = int(input("Enter 1, 2, or 3: "))
    global num_1
    if music == 1:
        num_1 = 3
    elif music == 2:
        num_1 = 2
    else:
        num_1 = 1

def question2():
    global num_2
    num_2 = 2

def question3():
    global num_3
    num_3 = 2

def main():
    question1()
    question2()
    question3()
    if num_1 + num_2 + num_3 == 9:
        print("Your favorite color is red")
    elif 5 < num_1 + num_2 + num_3 < 9:
        print("Your favorite color is blue")
    else:
        print("Your favorite color is green")

main()

替代解决方案(首选):我们不需要将三个变量定义为全局变量,只需在函数中返回它们,然后在main()函数中定义它们:

def question1():
    print("What's your favorite genre of music?")
    print("1. Pop")
    print("2. Rap")
    print("3. Metal")
    music = int(input("Enter 1, 2, or 3: "))
    if music == 1:
        num_1 = 3
    elif music == 2:
        num_1 = 2
    else:
        num_1 = 1
    return num_1

def question2():
    num_2 = 2
    return num_2
    

def question3():
    num_3 = 2
    return num_3

def main():
    num_1 = question1()
    num_2 = question2()
    num_3 = question3()
    if num_1 + num_2 + num_3 == 9:
        print("Your favorite color is red")
    elif 5 < num_1 + num_2 + num_3 < 9:
        print("Your favorite color is blue")
    else:
        print("Your favorite color is green")

main()

我希望这有助于回答你的问题!如果您需要任何进一步的澄清或详细信息,请告诉我:)

Python相关问答推荐

单击cookie按钮,但结果不一致

try 使用tensorFlow.keras.models时optree Import错误

合并其中一个具有重叠范围的两个框架的最佳方法是什么?

在Python中,如何才能/应该使用decorator 来实现函数多态性?

使用imap-tools时错误,其邮箱地址包含域名中的非默认字符

如何匹配3D圆柱体的轴和半径?

如何在vercel中指定Python运行时版本?

Polars Dataframe:如何按组删除交替行?

"如果发生特定错误,返回值

Image Font生成带有条形码Code 128的条形码时出现枕头错误OSErsor:无法打开资源

Pydantic 2.7.0模型接受字符串日期时间或无

Matlab中是否有Python的f-字符串等效物

Python上的Instagram API:缺少client_id参数"

OR—Tools CP SAT条件约束

try 将一行连接到Tensorflow中的矩阵

Asyncio:如何从子进程中读取stdout?

如何合并两个列表,并获得每个索引值最高的列表名称?

从Windows Python脚本在WSL上运行Linux应用程序

使用特定值作为引用替换数据框行上的值

在Admin中显示从ManyToMany通过模型的筛选结果