首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何要求无限输入,直到用户用所需的输入回复?(totalBill > 10)

如何要求无限输入,直到用户用所需的输入回复?(totalBill > 10)
EN

Stack Overflow用户
提问于 2021-12-31 23:26:52
回答 1查看 23关注 0票数 0

由于totalBill是-1,所以我的代码应该迭代其他语句。但是,根本没有输出。我已经编写了一个类似于以前的代码,其中我使用了附加列表的概念。我试着从这段代码中获得灵感,但没有奏效。

代码语言:javascript
复制
#Tip Calculator
totalBill = -1
while totalBill > 0:
    tip = int(input("What percentage would you like to tip? 10, 12, 15?"))
    if tip == 12 or tip == 10 or tip == 15:
        totalPeople = int(input("How many people should split the bill?"))
        eachBill = (totalBill + (totalBill * (tip / 100))) / totalPeople
        print(f"Each person must pay {eachBill}.")
    # else:
    #     print(f"Please enter a valid tip.")
    #     tip = int(input("What percentage would you like to tip? 10, 12, 15?"))
    else:
        print("Please enter a valid total bill.")
    totalBill = int(input("How much was the total bill?"))

我得到灵感的密码是:

代码语言:javascript
复制
#Appending to a list
#buyComputer.py

availableParts = ["Computer", "Monitor", "Keyboard", "Mouse", "Speaker"]
choice = "-"
computerParts = []

while choice != '0':
    if choice in "12345":
        print(f"Adding {availableParts[int(int(choice) - 1)]} to the list.")
        #index =
        #indexValue =
        if choice == '1':
            computerParts.append(availableParts[0])
        elif choice == '2':
            computerParts.append(availableParts[1])
        elif choice == '3':
            computerParts.append(availableParts[2])
        elif choice == '4':
            computerParts.append(availableParts[3])
        elif choice == '5':
            computerParts.append(availableParts[4])
    else:
        print("In order to start this program, please choose a valid input within 1-5.")
        # for part in availableParts: #Iterating though a list to display all the elements of a list
        #     position = availableParts.index(part) + 1
        #     print(position, part)
        for position, part in enumerate(availableParts):
            print(f"{position + 1}: {part}")
    choice = input("What part do you want to shop for?")
print(f"Your final list of items contains {computerParts}")
EN

回答 1

Stack Overflow用户

发布于 2021-12-31 23:29:11

永远不要输入while循环,因为输入的条件是总账单大于0

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70546206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档