Python 3状态跳闸程序:代码寄存器访问与一次跳闸相同的状态



我目前正在编写一个State Trip代码,记录您在Python 3中访问过的状态。如果我输入我访问了Nj的状态两次,我的代码将其注册为只访问Nj一次。我在下面提供了一张图片和我的全部代码:在此处输入图像描述

代码如下:

states = [ 'Nj', 'Fl', 'Ca', 'Ny', 'Ne', 
'De', 'Ri', 'Ia', 'Or', 'Co']
ask = input("Do you want to start a trip?[Y/n]: ")
visited_states = []
i = 1
if (ask.lower() == "y"):
print(states)
startstate = input("From which state, you want to start? ")
while startstate not in states:
startstate = input("From which state, you want to start? ")
visited_states.append(startstate)
while i < 10:
next_state = input("Enter next state: ")
while next_state not in states:
print("Please enter states from available states.")
next_state = input("Enter next state: ")
visited_states.append(next_state)
visit_more = input("Do you want to visit more?[Y/n]: ")
if visit_more.lower() != 'y':
break
i += 1
else:
exit(0)
print("Visited States: ", end="")
count = 0
for i in visited_states:
if i not in visited_states[:count]:
print(i, end=" ")
count += 1
print(f"nTotal states visited are: {count}")

上一个for循环中的if语句应该做什么?尝试删除它,它应该打印:

访问的州:Nj Nj
访问的州总数为:2

最新更新