首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >而输入不同于(letter1,字母2,.)这样做。否则就这么做

而输入不同于(letter1,字母2,.)这样做。否则就这么做
EN

Stack Overflow用户
提问于 2022-02-08 22:28:42
回答 1查看 20关注 0票数 -1
代码语言:javascript
复制
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)。

EN

回答 1

Stack Overflow用户

发布于 2022-02-08 22:34:39

代码语言:javascript
复制
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")

您可以添加一个循环,它可以工作。

代码语言:javascript
复制
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")
    break
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71041689

复制
相关文章

相似问题

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