使用多个输入语句与if/else语句
问题描述:
我想创建一个简单的程序基础上应该采取什么样的模式运人的,如果它取决于下雨工作多远他们生活:使用多个输入语句与if/else语句
< 2km = "walk"
> 10km = "bus"
>= 2km and <=10km = "bike"
我有找出了第一点(至少我认为我有),但我坚持如何整合不同距离的不同if语句。因此,一旦一个人对“今天下雨了”这个问题回答“否”,他们就必须输入一个整数来表示他们离开工作的距离,并据此告诉他们要采取什么样的运输方式。
任何帮助,将不胜感激
`weather = input("Is it raining today? ")
if weather == "yes":
print("You should catch the bus to work.")
elif weather == "no":
input("How far in km do you need to travel? ")`
答
有了这样的输入数据。有两种可能的基本解决方案:
def travel_choice(input)
return {
< 2 : “walk”;
2 > and < 10 : “bike”;
10 < : “bus"
} [input]
或
def travel_choice(input)
if (input < 2 and input > 0)
“walk”
elif(input > 2 and input < 10)
“bike”
elif “bus"
希望这将有助于
答
谢谢大家,这是一个很好的帮助(对不起,我是Python新手)。我认为刚刚得到两个输入语句的工作是主要问题,但我认为这应该解决我的问题:
weather = input("Is is currently raining today? ")
if weather == "yes":
print("You should take the bus to work.")
elif weather == "no":
x = int(input("How far in km do you live from work? "))
if x < 2:
print("You should walk to work.")
if x >10:
print("You should catch the bus to work.")
elif x >=2 and x <= 10:
print("You should ride your bike to work.")
你有什么试过?这似乎微不足道。您只需要比较上次输入和2,10,并根据比较结果打印结果 – Lafexlos