2017-06-14 101 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

相关问题