它显示的"str"是不可调用的,如何能够创建循环



我对这方面很陌生,我正在尝试循环。我想知道我做错了什么?我能做些什么来修复它?我会尝试另一种策略吗?请告诉我!这表明str是不可调用的,但我不知道这意味着什么?

def get_burger_choice():
burger_choice = input("Which one would you like? cheesy, regular, or veggie: ")
return burger_choice
def price_burger(burger_choice, burger_price):
if burger_choice == 'cheesy':
burger_price = 3.00
elif burger_choice == 'regular':
burger_price = 2.00
elif burger_choice == 'veggie':
burger_price = 2.00
else:
print("we do not have that, sorry")
return burger_price
def total_price(burger_price, burger_choice=None):
print("Your total is $", burger_price)

def closing(burger_choice):
if burger_choice == 'cheesy':
print("Nice selection, you've picked our best!")
else:
print("thank you")

def tryagain(main):
tryagain = input("nWould you like to try again? yes or no: ")
if tryagain == 'yes':
main()
elif tryagain == 'no':
print("n------------End---------------")
else:
oncemore(tryagain)

def oncemore(tryagain):
oncemore = input("nWould you like to try again? yes or no: ")
if tryagain == 'yes':
main()
elif tryagain == 'no':
print("n------------End---------------")
else:
tryagain(main)

def main(burger_price=0):
burger_choice = get_burger_choice()
burger_price = price_burger(burger_choice,burger_price)
total_price(burger_price)
closing(burger_choice)
tryagain(main)
oncemore(tryagain)
main()

问题从函数开始。这里你有一个函数再试一次,但一旦你输入,你就在输入,你正在创建一个新的变量;e同名。现在,这本身不会引起任何问题。但是,您将该变量传递给oncemore,它是一个字符串

def tryagain(main):
tryagain = input("nWould you like to try again? yes or no: ")
if tryagain == 'yes':
main()
elif tryagain == 'no':
print("n------------End---------------")
else:
oncemore(tryagain)

让我们再来看看函数。在这个函数中,您正在调用tryagain,它是字符串。因此,作为字符串的错误是不可调用的。

def oncemore(tryagain):
oncemore = input("nWould you like to try again? yes or no: ")
if tryagain == 'yes':
main()
elif tryagain == 'no':
print("n------------End---------------")
else:
tryagain(main)

解决方案:重命名变量,不要将函数作为参数传递。这在你的情况下是不需要的。所有函数都可以访问文件中的所有其他函数。

附言:你可以将你的主要功能更改为以下代码,使其更可读

def main():

choice = "yes"
while choice is not "no":
burger_choice = get_burger_choice()
burger_price = price_burger(burger_choice,burger_price)
total_price(burger_price)
closing(burger_choice)
choice = input("nWould you like to try again? yes or no: ")

相关内容

最新更新