使用if / else的多个输入语句

时间:2016-02-26 09:50:14

标签: python if-statement input logic

我正在尝试创建一个简单的程序,根据人们应该采取什么样的交通工具,如果下雨取决于他们居住的距离:

< 2km = "walk"
> 10km = "bus"
>= 2km and <=10km = "bike"

我已经计算出第一位(至少我认为我有),但我仍然坚持如何为不同的距离整合不同的if语句。因此,一旦一个人回答&#34;没有&#34;对于问题&#34;今天是否正在下雨&#34;,然后他们必须输入一个整数来表示他们离开工作的距离,并在此基础上告诉他们采取什么样的运输方式。

非常感谢任何帮助

`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? ")`    

2 个答案:

答案 0 :(得分:0)

有了这样的输入数据,有两种可能的基本解决方案:

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"

希望这会有所帮助

答案 1 :(得分:0)

感谢大家,这是一个很好的帮助(对不起,我是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.")