2017-06-14 102 views
1

我是一個使用CodeCademy自學Python的新手程序員。我自己寫了一個腳本來檢查我迄今爲止學到的知識。此腳本的目的是根據用戶輸入的日期打印出某個週末可用的人員姓名,並與我在腳本中編寫的日期列表進行交叉引用。腳本在CodeCademy工作,但不在命令行

奇怪的是,這個腳本的功能與CodeCademy的Python環境中的預期功能完全一樣,沒有錯誤。它完全返回我期望的每一次結果。但是,當我嘗試在我的計算機上通過命令行使用Python 3.6.1手動運行腳本時,情況並非如此。相反,無論如何,它每次都會返回相同的結果。這裏是我的代碼:

#script to tell who is free on a certain weekend 
input_date = input("Please input the weekend on which you are looking for in   
the format mm.dd (ex. weekend of June 30th is 06.30): ") 
ben_dates = [06.16,06.23,06.30,07.07,07.14,08.04,08.11] 
david_dates = [06.16,06.23,06.30,07.14,07.28,08.04,08.11] 
danyall_dates = [06.30,07.07,07.14,07.21,07.28,08.04,08.11] 
kevin_dates= [06.16,06.23,06.30,07.07,07.14,07.21,07.28,08.04,08.11,08.18] 
manan_dates=[06.16,07.14,07.21,07.28,08.04] 
jack_dates=[06.30,07.07,07.14,07.21,07.28,08.04] 

free_people = "The people free on this date are: " 
free_people_orig = free_people 


for date in ben_dates: 
    if input_date == date: 
    free_people = free_people + "Ben, " 


for date in david_dates: 
    if input_date == date: 
    free_people = free_people + "David, " 

for date in danyall_dates: 
    if input_date == date: 
    free_people = free_people + "Danyall, " 

for date in kevin_dates: 
    if input_date == date: 
    free_people = free_people + "Kevin, " 

for date in manan_dates: 
    if input_date == date: 
    free_people = free_people + "Manan, " 

for date in jack_dates: 
    if input_date == date: 
    free_people = free_people + "Jack, " 

if len(free_people) == len(free_people_orig): 
    free_people = "No one is free on this weekend." 

print(free_people) 

因此,舉例來說,如果用戶輸入'06 0.30' 上Codecademy網站,該程序將打印「的人在這一天免費是:本,大衛,Danyall,凱文·傑克, '這將是正確的結果。

但是,如果在命令行中運行,相同的輸入將打印出'本週末沒有人免費',我完全不知道爲什麼會發生這種情況。

我已經嘗試了while和for循環的幾種不同變體,使用if,elif和else語句,更改free_people字符串的條件和格式以及觸發它的修改方式以及其他許多其他策略關於這個特定的解決方案,還沒有人能夠使腳本正常運行。我在這裏做錯了什麼,它在CodeCademy中工作,但不在我的電腦上?

此外,我知道這遠不是爲此任務創建腳本的最佳方式,即使此時我的實現當然可能會更好。然而,我是一名初學者,並且正在編寫這個腳本,主要考慮測試我通過編寫腳本所學到的特定技能,這個腳本可能對我有一些基本的用處。我只想知道爲什麼這個特定腳本的特定版本不起作用。

P.S.這是我在StackOverflow上的第一篇文章,如果我錯誤地格式化了這篇文章,我很抱歉。

+1

Input_date是'str'和你試圖將它與'float's比較。 – abccd

回答

4

問題在於,當您需要成爲浮點數時,您正在輸入一個字符串。列表中的每個元素都是浮動元素,並且您正在嘗試查看是否存在任何這些列表中的字符串類型的元素,即False

試試這個:

input_date = float(input("Please input the weekend on which you are looking for in the " 
         "format mm.dd (ex. weekend of June 30th is 06.30): ")) 
+0

這完全解決了這個問題,非常感謝! – MattO

+0

不客氣。請考慮將此回覆標記爲已回覆,以便其他用戶知道您的問題有答案。謝謝。 – Ajax1234

+0

我知道,我會盡快接受這個答案。 StackOverflow阻止我選擇10分鐘的答案,並且該時間段尚未結束。謝謝 – MattO

相關問題