2016-03-15 44 views
-1

我如何要求用戶輸入有多遠?我如何要求用戶輸入有多遠?

例如

"How far away is the closest person: " 

,然後得到它告訴我,如果600米

例如下

"Canon start Rocket Lift Off because the closest person is less than 600m away 
+0

你嘗試過什麼嗎?請[編輯]你的問題,幷包括你嘗試過的代碼。 – 2016-03-15 06:47:35

回答

1

使用Python的input()方法。

>>> a = input("Input the distance: ") 
Input the distance: 800 
>>> print(a) 
800 

對於600米的要求,請閱讀if statements

>>> a = "test" 
>>> if a == "test": 
...  print("true") 
... 
true 
0

詢問用戶一個問題,這樣做:

x = raw_input("How far away is the closest person: ") 

爲Python 2.7或

x = input("How far away is the closest person: ") 

爲Python 3

,然後你可能會制定了現在:

if x < 600: 
    # do something 
0

只需使用print()來顯示信息,input()來接收輸入信息,並使用if來判斷距離是否足夠。 float(input())表示將您輸入的內容轉換爲浮點型對象。

print "How far away is the closest person: " 
if float(input()) <= 600: 
    print "Canon start Rocket Lift Off because the closest person is less than 600m away" 
相關問題