我正在编写一个程序,当用户第一次输入“apple”时,它将打印第0天和列表中的第一个元素。当用户第二次输入“apple”时,将打印第一天和第二个元素。我想重复30次,持续30天。
我的代码:
list_1=["a", "b", "c", "d", "e", "f", "g"]
index = 6
a = 0
i = 0
while True:
a = input("Enter:")
if a == "apple":
if i < 31:
index = (index + 1) % 7
d = list_1[index]
print( "day" ,i, d )
start = input("Start: ")
current = input("Current: ")
i += 1
a += 1
我的输出:
Enter:apple
day 0 a
Start: 2
Current: 3
day 1 b
Start: 2
Current: 3
day 2 c
Start: 2
Current: 3 ... ... ...
期望输出:
Enter:apple
day 0 a
Start: 2
Current: 3
Enter:apple
day 1 b
Start: 2
Current: 3 ... ... ... ... Enter:apple
day 6 g
Start: 2
Current: 3
Enter:apple
day 7 a
Start: 2
Current: 3
... ... ... ...
Enter:apple
day 30 c
Start: 2
Current: 3
您必须将i和index都移出循环。
list_1=["a", "b", "c", "d", "e", "f", "g"]
i = 0
index = 6
while True:
a = input("Enter:")
if a == "apple":
a = 0
if i < 31:
index = (index + 1) % 7
d = list_1[index]
print( "day" ,i, d )
start = input("Start: ")
current = input("Current: ")
i = i + 1
a = a + 1
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。