脚本适用于CodeCademy,但不适用于命令行

时间:2017-06-14 15:15:22

标签: python command-line python-3.6

我是一名新手程序员,使用CodeCademy自学Python。我自己编写了一个脚本,以便检查我到目前为止所学到的知识。此脚本的目的是根据用户输入日期打印某个周末可用的人员姓名,并与我在脚本中写入的日期列表进行交叉引用。

奇怪的是,这个脚本完全按照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.30'在CodeCademy上,该计划将打印出这个日期免费的人是:Ben,David,Danyall,Kevin,Jack,'这将是正确的结果。

但是,如果在命令行中运行,则会打印相同的输入,并且“这个周末没有人免费”。我完全不知道为什么会这样。

我尝试了while和for循环的几种不同变体,使用if,elif和else语句,改变了free_people字符串的条件和格式以及触发它的修改方法,以及许多其他的策略这个特定的解决方案,但没有一个能够使脚本正常运行。我在这里做错了它在CodeCademy中工作但在我的电脑上却没有?

另外,我知道这远不是为此任务创建脚本的最佳方法,即便如此,我的实现肯定会更好。但是,我是初学者,我正在编写这个脚本,主要关注的是测试我学到的特定技能,通过编写一个脚本,可以为我自己提供一些基本用法。我只想弄清楚为什么这个特定脚本的特定版本不起作用。

P.S。这是我在StackOverflow上的第一篇文章,如果我错误地格式化这篇文章,我很抱歉。

1 个答案:

答案 0 :(得分: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): "))