Python变量未在if语句中注册



当我问用户是否愿意超大订单时,python说这是一个无效条目。别介意这个选项表,它在别的地方用过了。

print("What size would you like?nSmall ($1.50)nMedium ($2.50)nLarge ($3.50)n")
main_choice=input("-").lower()
if main_choice=="small":
choice.append("small")
money+=1.50
pass
elif main_choice=="medium":
choice.append("medium")
money+=2.50
pass
elif main_choice=="large":
choice.append("large")
money+=3.50
os.system('clear')
t.sleep(1)
print("Would you like to Mega-Size your order for an extra $0.50? (yes/no)")
main_choice=input("-").lower
os.system('clear')
if main_choice=="yes":
money+=0.50
choice[2]="Mega-Size"
pass
elif main_choice=="no":
pass
else:
os.system('clear')
print(Fore.RED+"That is a invalid entry please try again.")
print(Style.RESET_ALL)
t.sleep(2)
os.system('clear')
fries_order()
else:
os.system('clear')
print(Fore.RED+"That is a invalid entry please try again.")
print(Style.RESET_ALL)
t.sleep(2)
os.system('clear')
fries_order()
os.system('clear')
str_money=str(money)
print("You ordered a",choice[0],"sandwich.")
print("You ordered a",choice[1],"drink.")
print("You ordered a",choice[2],"fry.")
print("Your cost so far is: $"+str_money)

我曾试图使main_choice变为全球性的,但没有成功。

您没有在中调用lower()

main_choice=input("-").lower

所以CCD_ 2不是字符串而是绑定函数lower:

>>> main_choice=input("-").lower
-foo
>>> main_choice
<built-in method lower of str object at 0x106bebeb0>
>>> main_choice == "foo"
False
>>>

–您需要

main_choice = input("-").lower()

调用lower()并让它返回小写字符串供您分配。

>>> main_choice=input("-").lower()
-foo
>>> main_choice
'foo'
>>> main_choice == "foo"
True
>>>

最新更新