2017-08-02 19 views
-3

我想獲得用戶輸入的東西,但它不是非常用戶友好的每次輸入答案時都必須加上引號。當給用戶輸入時,我不想要引用這是可能的嗎?

loop = True 
    while loop: 

     user_input = input("say hello \n") 
     if user_input == "hello": 
     print("True") 

我希望能夠輸入hello而不必添加引號(例如「hello」)。

+0

你不會有。運行該程序,它應該沒有報價工作。 –

+1

升級到python 3.或者,使用'raw_input'。 –

+1

這是蟒蛇2.x?如果是這樣的話,那麼使用'raw_input' – Wondercricket

回答

1

在Python 3.6交互提示:

>>> user_input = input("say hello \n") 
say hello 
hello 
>>> user_input 
'hello' 

你需要引用的字符串時,你檢查的平等,而是應該不需要爲輸入。正如其他人所說,在python 2.x中,使用raw_input。 Python 2.7版:

>>> user_input = raw_input("say hello \n") 
say hello 
hello 
>>> user_input 
'hello' 
0

有一個在輸入的raw_input略有區別:What's the difference between raw_input() and input() in python3.x?

蟒蛇2.7:

loop = True 
hello_input = "hello" 
while loop: 
    user_input = raw_input("say hello \n") 
    if user_input == hello_input: 
     print("True") 

蟒蛇3:

loop = True 
hello_input = "hello" 
while loop: 
    user_input = input("say hello \n") 
    if user_input == hello_input: 
     print("True") 

當您運行這些程序你不必把輸入放在引號中:

如:

say hello 
hello 
True 
+1

如果你認爲一個問題是重複的,那麼你應該這樣標記它,因爲沒有辦法知道是否這確實是問題。 – Sayse

+1

@Sayse問題不是重複的,因爲Asker不知道他/她正在尋找的是針對同一問題的不同方法。即:使用'raw_input'而不是'input'。 –

相關問題