2017-05-27 13 views
0

下面的程序應該把單詞放在一個列表中。測試它的工作是'print(allwords [1])'。有趣的是底部工作,但是當我使用頂部時,它不會。該列表顯然保持空白。我在做什麼與衆不同?兩個非常類似的object.extend()代碼來填充一個列表,一個作品一不是

# DOES NOT WORK 
print ('On what difficulty do you want to play?\n') 
diffculty = input('1.Easy\n2.Medium\n3.Hard\n\n') 
if diffculty == 1 or input == 'Easy' or input == 'easy' or input == 'EASY': 
    with open('google-10000-english-usa-no-swears-short.txt') as inputfile: 
     for line in inputfile: 
      allwords.extend(line.strip().split(',')) 
elif diffculty == 2 or input == 'Medium' or input == 'medium' or input == 'MEDIUM': 
    with open('google-10000-english-usa-no-swears-medium.txt') as inputfile: 
     for line in inputfile: 
      allwords.extend(line.strip().split(',')) 
elif diffculty == 3 or input == 'Hard' or input == 'hard' or input == 'HARD': 
    with open('google-10000-english-usa-no-swears-long.txt') as inputfile: 
     for line in inputfile: 
      allwords.extend(line.strip().split(',')) 

# WORKS 
print ('Okay then, let us begin with your first word.\n') 
with open('google-10000-english-usa-no-swears-long.txt') as inputfile: 
    for line in inputfile: 
     allwords.extend(line.strip().split(',')) 

print (allwords[1]) 

所以問題實際上是IF語句。我做了兩個改變,現在它的工作。 1.我做了if語句檢查字符串數字('1') 2.我更正了if變量(它們應該都是差異性的) 感謝您的幫助!

+0

帶'if語句'的代碼是否運行?你有沒有嘗試在那裏打印一些東西進行測試? –

+0

這是一個非常好的問題。我剛剛檢查過(每次if後都打印1,2,3),但他們沒有。但嚴重的是,他們爲什麼不呢? –

+0

你在'if語句'中的第一個比較是錯誤的。你試圖比較一個字符串到一個int –

回答

1

更改if statement

嘗試用

if diffculty == '1' or input == 'Easy' or input == 'easy' or input == 'EASY': 

注意的是1串withing現在

input存儲爲一個字符串,你正試圖將其與int比較放置它。

另外,就像在他的回答中提到的@zwer一樣,最好將用戶輸入轉換爲小寫以便比較。所以,你不必擔心比較同一個單詞的多種可能性。你可以嘗試這樣的:

print ('On what difficulty do you want to play?\n') 
diffculty = input('1.Easy\n2.Medium\n3.Hard\n\n') 

#The code below converts the user input to lower case. If example the user types 
# HARD or Hard or harD or something it converts it to 'hard' 

diffculty = diffculty.lower() 

print(diffculty) 

if diffculty == '1' or diffculty == 'easy': 
    print('easy') 
    #do your easy code 
elif diffculty == '2' or diffculty == 'medium': 
    print('medium') 
    #do your medium code 
elif diffculty == '3' or diffculty == 'hard': 
    print('hard') 
    #do your hard code 
else: 
    print('Please enter the correct difficulty level') 
+0

你的答案解決了這個問題,因爲和我以前的評論一樣。謝謝! –

+0

不客氣。請檢查我最近編輯的答案 –

0

input()給你一個字符串,所以你需要把它轉換成一個int做你difficulty == 1和類似檢查前:

difficulty = int(input('1.Easy\n2.Medium\n3.Hard\n\n')) 

您可能要添加一些額外的驗證。

此外,input()是一個函數,因此檢查input == 'Hard'是否沒有意義。如果要考慮從用戶輸入「所有」不測事件,這樣做:

difficulty = input('1.Easy\n2.Medium\n3.Hard\n\n').lower() # turn input to lowercase 
if difficulty in ("1", "easy"): 
    # your easy code 
elif difficulty in ("2", "medium"): 
    # your medium code 
elif difficulty in ("3", "hard"): 
    # your hard code 
else: 
    # wrong input... 
+0

它應該是一個用戶輸入來改變遊戲的難度。你能解釋爲什麼它沒有意義嗎? –

+0

@TomKisters - 你在'if'語句中的所有後續檢查中都使用'input'而不是'difficulty'變量(它應該是'hard =='Hard'',而不是'input =='Hard''等等)。可能是一個錯字。無論哪種方式,你都需要像like一樣比較,所以按照上面的方法 - 只處理字符串。 – zwer

+0

我使用了下面的答案,如果我正確的話也解決了你提到的問題。非常感謝您的幫助! –

相關問題