umbrella = input("Which umbrella are you going to use? Select between a,b,c,d,e \n")
if umbrella == "a":
amount_new_umbrellas = math.ceil(x / 45500)
print("umbrellas type a: "+ str(amount_new_umbrellas))
elif umbrella == "b":
amount_new_umbrellas = math.ceil(x / 16700)
print("umbrellas type b: "+ str(amount_new_umbrellas))
elif umbrella == "c":
amount_new_umbrellas = math.ceil(x / 27800)
print("umbrellas type c: "+ str(amount_new_umbrellas))
elif umbrella == "d":
amount_new_umbrellas = math.ceil(x / 7600)
print("umbrellas type d: "+ str(amount_new_umbrellas))
elif umbrella == "e":
amount_new_umbrellas = math.ceil(x / 13800)
print("umbrellas type e: "+ str(amount_new_umbrellas))
else:
print("Data Error")我需要使用WHILE循环(主循环)来改进代码,以不断向用户请求输入(a,b,c,d,e)。
发布于 2022-02-08 22:34:39
import math
umbrella = input("Which umbrella are you going to use? Select between a,b,c,d,e \n")
x = 100
amount_new_umbrellas = {
"a" : math.ceil(x / 45500),
"b" : math.ceil(x / 16700),
"c" : math.ceil(x / 27800),
"d" : math.ceil(x / 7600),
"e" : math.ceil(x / 13800),
}.get(umbrella)
if amount_new_umbrellas:
print("umbrellas type "+str(umbrella)+": "+ str(amount_new_umbrellas))
else:
print("Data Error")您可以添加一个循环,它可以工作。
import math
while True:
umbrella = input("Which umbrella are you going to use? Select between a,b,c,d,e \n")
x = 100
amount_new_umbrellas = {
"a" : math.ceil(x / 45500),
"b" : math.ceil(x / 16700),
"c" : math.ceil(x / 27800),
"d" : math.ceil(x / 7600),
"e" : math.ceil(x / 13800),
}.get(umbrella)
if amount_new_umbrellas:
print("umbrellas type "+str(umbrella)+": "+ str(amount_new_umbrellas))
else:
print("Data Error")
breakhttps://stackoverflow.com/questions/71041689
复制相似问题