如果要求不满足,如何进行输入循环?

时间:2017-10-25 23:36:39

标签: python python-3.x loops

因此,Tip_Percentage必须高于15,并且我希望输入循环,如果不满足它的要求,我是如何实现的?

Meal_Cost = int(input("Meal Cost: $"))
Patrons = int(input("Number of Patrons: "))
Tip_Percentage = int(input("Tip %: "))

while Tip_Percentage <= 15:
    print("Please give a higher tip than that.")
    break

else:
  if Tip_Percentage >= 15:
    print('\n')


  Price_of_Meal = Meal_Cost / Patrons
  print("Price of Meal: $", Price_of_Meal,)


  Tip_Amount = (Tip_Percentage / 100 * Meal_Cost)
  print("Tip: $", Tip_Amount)

2 个答案:

答案 0 :(得分:1)

再次询问Tip_Percentage输入:

Tip_Percentage = int(input("Tip %: "))

while Tip_Percentage <= 15:
    print("Please give a higher tip than that.")
    Tip_Percentage = int(input("Tip %: "))

答案 1 :(得分:0)

你必须要求提示,直到它大于或等于15。

Meal_Cost = int(input("Meal Cost: $"))
Patrons = int(input("Number of Patrons: "))
Tip_Percentage = int(input("Tip %: "))

while Tip_Percentage <= 15:
    Tip_Percentage = int(input("Please give a higher tip than that : "))

Price_of_Meal = Meal_Cost / Patrons
print("Price of Meal: $", Price_of_Meal,)


Tip_Amount = (Tip_Percentage / 100 * Meal_Cost)
print("Tip: $", Tip_Amount)