【问题标题】:Python returning multiple values from a function to anotherPython将多个值从一个函数返回到另一个
【发布时间】:2021-12-16 16:26:44
【问题描述】:

我正在尝试从我的 getValues 函数中获取 2 个值并将其作为参数返回给我的 askQuestion 函数

我收到一个错误:

TypeError: askQuestion() 缺少 1 个必需的位置参数:'curA'

#getting a question and index for the answer
def getValues():
    curQ = r.choice(qs)
    index = qs.index(curQ)
    curA = ans[index]
    #returning question and answer to next function
    return curQ, curA
    
#prompting the user with question for answer
def askQuestion(curQ, curA):
    userA = input(question + ": ")
    userA.upper()
    userA.replace(" ", "")
    #using variable because it wont let me carry it over
    answer.append('')
    #returning answer and user's answer
    return curA, userA

askQuestion(getValues())

【问题讨论】:

  • getValues 返回一个包含 (curQ, curA) 的列表,即 1 个变量。您可以更改您的 askQuestion() 函数以获取数组输入
  • @JacksonB 这是一个元组,不是列表或数组。

标签: python return


【解决方案1】:

使用扩展运算符将返回元组分隔为单独的参数。

askQuestion(*getValues())

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多