使用以下代碼時:url = Noneprint("For 'The Survey of Cornwall,' press 1")print("For 'The Adventures of Sherlock Holmes,' press 2")print("For 'Pride and Prejudice,' press 3")n = input("Which do you choose?")if n==1: url = 'http://www.gutenberg.org/cache/epub/9878/pg9878.txt' #cornwall print("cornwall")elif n==2: url = 'http://www.gutenberg.org/cache/epub/1661/pg1661.txt' #holmes print("holmes)elif n==3: url = 'http://www.gutenberg.org/cache/epub/1342/pg1342.txt' #pap print("PaP")else: print("That was not one of the choices")我只返回“ else”案例,為什么會這樣呢?
4 回答

臨摹微笑
TA貢獻1982條經驗 獲得超2個贊
input()返回py3x中的字符串。因此,您需要將其轉換為intfirst。
n = int(input("Which do you choose?"))
演示:
>>> '1' == 1
False
>>> int('1') == 1
True

12345678_0001
TA貢獻1802條經驗 獲得超5個贊
input()
返回一個字符串,但是您正在將其與整數(shù)進行比較。您可以使用int()
函數(shù)將輸入的結果轉換為整數(shù)。

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
input()返回一個字符串類型。因此,您需要使用int()將輸入轉換為整數(shù),否則可以將輸入與字符而不是整數(shù)進行比較,例如“ 1”,“ 2”。

婷婷同學_
TA貢獻1844條經驗 獲得超8個贊
您應該使用int()將輸入轉換 n = input("Which do you choose?")
為。n = int(input("Which do you choose?"))
這是由于輸入幾乎返回始終可以正常工作的事實,所以輸入返回所有輸入的字符串。
添加回答
舉報
0/150
提交
取消