脚本在 CodeCademy 中有效,但在命令行中无效
Script works in CodeCademy but not at command line
我是一名新手程序员,自学 Python 使用 CodeCademy。我自己写了一个脚本来检查我到目前为止学到的知识。该脚本的目的是根据用户输入的日期打印某个周末有空的人员姓名,并与我在脚本中编写的日期列表进行交叉引用。
奇怪的是,这个脚本在 CodeCademy 的 Python 环境中完全按照预期运行,没有任何错误。它 returns 正是我每次都期望的结果。但是,当我尝试通过命令行在计算机上使用 Python 3.6.1 手动 运行 脚本时,情况并非如此。相反,它 returns 无论如何每次都会得到相同的结果。这是我的代码:
#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)
因此,例如,如果用户在 CodeCademy 上输入“06.30”,程序将打印 'The people free on this date are: Ben, David, Danyall, Kevin, Jack,',这将是正确的结果。
但是,如果在命令行中 运行,相同的输入将打印 'No one is free on this weekend',我完全不知道为什么会这样。
我尝试了 while 和 for 循环的几种不同变体,使用 if、elif 和 else 语句,更改 free_people 字符串的条件和格式以及触发它被修改的内容,以及许多其他策略来解决这个特定的解决方案,但 none 已经能够正确地制作脚本 运行。它在 CodeCademy 中有效但在我的计算机上无效,我在这里做错了什么?
此外,我知道这远不是为此任务创建脚本的最佳方式,即便如此,我的实施方式肯定会更好。但是,我是初学者,编写此脚本的主要目的是测试我通过编写可能对自己有一些基本用途的脚本而学到的特定技能。我只想弄清楚为什么这个特定脚本的这个特定版本不起作用。
P.S。这是我在 Whosebug 上的第一个 post,如果我的 post 格式不正确,我深表歉意。
问题是您输入的是一个字符串,而它需要是一个浮点数。列表中的每个元素都是浮点数,并且您正在尝试查看这些列表中是否存在字符串类型的元素,即 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): "))
我是一名新手程序员,自学 Python 使用 CodeCademy。我自己写了一个脚本来检查我到目前为止学到的知识。该脚本的目的是根据用户输入的日期打印某个周末有空的人员姓名,并与我在脚本中编写的日期列表进行交叉引用。
奇怪的是,这个脚本在 CodeCademy 的 Python 环境中完全按照预期运行,没有任何错误。它 returns 正是我每次都期望的结果。但是,当我尝试通过命令行在计算机上使用 Python 3.6.1 手动 运行 脚本时,情况并非如此。相反,它 returns 无论如何每次都会得到相同的结果。这是我的代码:
#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)
因此,例如,如果用户在 CodeCademy 上输入“06.30”,程序将打印 'The people free on this date are: Ben, David, Danyall, Kevin, Jack,',这将是正确的结果。
但是,如果在命令行中 运行,相同的输入将打印 'No one is free on this weekend',我完全不知道为什么会这样。
我尝试了 while 和 for 循环的几种不同变体,使用 if、elif 和 else 语句,更改 free_people 字符串的条件和格式以及触发它被修改的内容,以及许多其他策略来解决这个特定的解决方案,但 none 已经能够正确地制作脚本 运行。它在 CodeCademy 中有效但在我的计算机上无效,我在这里做错了什么?
此外,我知道这远不是为此任务创建脚本的最佳方式,即便如此,我的实施方式肯定会更好。但是,我是初学者,编写此脚本的主要目的是测试我通过编写可能对自己有一些基本用途的脚本而学到的特定技能。我只想弄清楚为什么这个特定脚本的这个特定版本不起作用。
P.S。这是我在 Whosebug 上的第一个 post,如果我的 post 格式不正确,我深表歉意。
问题是您输入的是一个字符串,而它需要是一个浮点数。列表中的每个元素都是浮点数,并且您正在尝试查看这些列表中是否存在字符串类型的元素,即 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): "))