由于totalBill是-1,所以我的代码应该迭代其他语句。但是,根本没有输出。我已经编写了一个类似于以前的代码,其中我使用了附加列表的概念。我试着从这段代码中获得灵感,但没有奏效。
#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?"))我得到灵感的密码是:
#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}")发布于 2021-12-31 23:29:11
永远不要输入while循环,因为输入的条件是总账单大于0
https://stackoverflow.com/questions/70546206
复制相似问题