简体   繁体   English

我怎么能强迫它只串

[英]how can i force to make it to only string

Hello How can i force to make the nname to only string input because even though i put number it still continue to the next function and i only like it to be a name of the people nname= str(input()) does not work thats why im asking if there is other alternative to make it work with only a 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()

When using the logical "or" operator in an if...else statement in python you have to repeat the variable as well as the value (i know that sounds confusing so look at the example)在 python 的 if...else 语句中使用逻辑“或”运算符时,您必须重复变量和值(我知道这听起来令人困惑,所以请看示例)

this means that this won't work:这意味着这不起作用:

if order == "Milktea" or "milktea":

the correct formation should be:正确的编队应该是:

if (order == "Milktea") or (order == "milktea"):

alternatively if you want to simply check one variable against a range of values check out the match...case statements in python 3.10+: https://learnpython.com/blog/python-match-case-statement/或者,如果您只想根据一系列值检查一个变量,请查看 match...case 语句 python 3.10+: https://learnpython.com/blog/python-match-case-statement/

###################################################### ################################################# ####

Remove the if order in mmu line and indent the corresponding else (code listed below)删除 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

you can combine this with my answer above to get what you need:)你可以把它和我上面的答案结合起来得到你需要的东西:)

You can use either lower() or upper().您可以使用lower() 或upper()。 Please check the following answer:请检查以下答案:

 if order.lower()=="milktea":
       print(flvrs)
       laht.append(order)
       break

OR或者

 if order.upper()=="MILKTEA":
       print(flvrs)
       laht.append(order)
       break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何只匹配单词并使它们成为带有标签集的字符串? - how can I match only the word and make them string within the string with tag-set? 如何强制simpleblobdetection仅在轮廓区域内搜索? - How can I force simpleblobdetection to only search inside a contour area? 当我连接两个字符串系列时,我可以强制 Python 仅以字符串格式返回吗? - Can I force Python to return only in String-format when I concatenate two series of strings? 如何使 get_random_string Django 模块仅返回数值? - How can I make the get_random_string Django module to return only numerical values? 我怎样才能设法使这个输入只有整数回答和被拒绝的字符串。 这包括.split() - How can I manage to make this input only integers answer and rejected string. This include .split() 我怎样才能使我的字符串搜索正确,因为它只显示第一次命中? - How can i make my string search correct because it only shows the first hit? 我如何确保用户输入的字符串仅包含数字,运算符和字母Q? - how can i make sure that the user inputs a string containing only numbers, operators, and letter Q? Python 如何将 int 转换为字符串? - Python how can I make an int to a string? 我怎样才能使字符串上下 - How can I make string upper and lower 我怎样才能使'string'到数组为真? - How can I make 'string' to array true?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM