例如我正在为学校工作的代码在我在此函数中添加以下多个返回时给出了一个元组错误
def GetHowLongToRun():
print('Welcome to the Plant Growing Simulation')
print()
print('You can step through the simulation a year at a time')
print('or run the simulation for 0 to 5 years')
print('How many years do you want the simulation to run?')
Years = int(input('Enter a number of years to run, or -1 for stepping mode:'))
skipyear = input("Would you like to pause every year? Y/N")
skipseason = input("Would you like to pause every season? Y/N")
return Years, skipyear, skipseason
这给了我以下错误
Traceback (most recent call last):
File "C:\Users\jonwe\Downloads\Paper1_ASLv1_2017_Python3_Pre (1).py", line 170, in <module>
Simulation()
File "C:\Users\jonwe\Downloads\Paper1_ASLv1_2017_Python3_Pre (1).py", line 154, in Simulation
if YearsToRun >= 1:
TypeError: '>=' not supported between instances of 'tuple' and 'int'
但如果我只是返回“年”字符串,它不会给我任何错误并且完美地运作
答案 0 :(得分:0)
我认为问题在于如何处理/解压缩返回值。我不知道你现在有什么,但它应该是:
YearsToRun, skipy, skips = GetHowLongToRun ()
为函数指定变量的顺序应与返回值的顺序相同。
>>> YearsToRun, skipy, skips = GetHowLongToRun ()
Welcome to the Plant Growing Simulation
You can step through the simulation a year at a time
or run the simulation for 0 to 5 years
How many years do you want the simulation to run?
Enter a number of years to run, or -1 for stepping mode:2
Would you like to pause every year? Y/NN
Would you like to pause every season? Y/NN
>>> if YearsToRun >= 1:
... print ('Do something.')
... print ('Skip year: ', skipy)
... print ('Skip season: ', skips)
...
Do something.
Skip year: N
Skip season: N