![](/img/trans.png)
[英]how can I match only the word and make them string within the string with tag-set?
[英]how can i force to make it to only string
您好,我如何强制将 nname 设置为仅输入字符串,因为即使我输入数字,它仍然会继续到下一个 function 并且我只喜欢它是人名 nname= str(input()) 不起作用为什么我要问是否有其他选择可以让它只使用一个字符串
print("Good Day Welcome To our Shop.")
mnu = ["Milktea","Foods", "Drinks", "Dessert"]
lsa = ["1.Matcha", "Taro", "Winter Melon", "Okinawa", "Chocolate",
"Cheese Cake"]
fds = {'Chicken'}
shot = ["tubig"]
mtms = ["ice cream"]
laht = []
print("Hi what would you like to be called, "
"So we can inform you if your order is ready")
def cstmrinfo(name):
print("Okay " + name, "So what would you like to get " )
print(*mnu,sep = "\n")
nname= input().lower()
def kuha_order():
while True:
order = input()
if (order == "Milktea") or (order == "milktea"):
print(lsa)
laht.append(order)
break
elif (order == "Foods") or (order == "foods"):
print(fds)
laht.append(order)
break
elif (order == "Drinks") or (order == "drinks"):
print(shot)
laht.append(order)
break
elif (order == "Dessert") or (order == "dessert"):
print(mtms)
laht.append(order)
break
else:
print("Sorry you input a thing that is not available on our menu, "
"Please Try again:")
continue
def pnglhtn():
while True:
print("I Would like to get a: ")
qwe = input()
if qwe in lsa:
print(qwe)
elif qwe in fds:
print(qwe)
elif qwe in shot:
print(qwe)
elif qwe in mtms:
print(qwe)
else:
print("There is no such thing like that ")
continue
dmi = int(input("How Many Servings Would you Like: "))
laht.append(qwe)
laht.append(dmi)
print("So " + pngln, "you Like a " + str(laht[:2]))
print (dmi, "Serves of: " + str(laht[:2]))
break
cstmrinfo(nname)
kuha_order()
pnglhtn()
在 python 的 if...else 语句中使用逻辑“或”运算符时,您必须重复变量和值(我知道这听起来令人困惑,所以请看示例)
这意味着这不起作用:
if order == "Milktea" or "milktea":
正确的编队应该是:
if (order == "Milktea") or (order == "milktea"):
或者,如果您只想根据一系列值检查一个变量,请查看 match...case 语句 python 3.10+: https://learnpython.com/blog/python-match-case-statement/
################################################# ####
删除 mmu 行中的 if 顺序并缩进对应的 else(代码如下)
def kuha_order():
while True:
order = input()
print(order)
if order == "Milktea":
print(flvrs)
laht.append(order)
break
elif order == "Foods":
print(pgkn)
laht.append(order)
break
elif order == "Drinks":
print(shot)
laht.append(order)
break
elif order == "Dessert":
print(mtms)
laht.append(order)
break
else:
print("Sorry you input a thing that is not available on our menu")
continue
你可以把它和我上面的答案结合起来得到你需要的东西:)
您可以使用lower() 或upper()。 请检查以下答案:
if order.lower()=="milktea":
print(flvrs)
laht.append(order)
break
或者
if order.upper()=="MILKTEA":
print(flvrs)
laht.append(order)
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.