'My for loop breaks and repeats the output over and over, also I cannot use Dictionaries in this
My code is a WW2 fact machine and for example if you input "1939" the code should print "start of war". If you input a keyword like "Dunkirk" it should work the same way and print the corresponding index from the facts list. But right now if I were to type 1939 or any input in fact, it will print the same output over and over. I know dictionaries would make it simple but I cannot use them in this assignment.
#Keyword facts
facts = ["start of war","Germany invades Norway + Denmark","USA declares war against
Japanese","USA begins raiding Japanese islands","Allies invade Scilly and put
Mussolini out of power","Battle of Normandy","May 8th 1945 Germany surrendered"]
factsK = ["The deadliest battle in the War, Germany and its allies fought the soviets
for the city with extreme close quarters combat and direct air raids on civilians",
"Was a German military tactic calculated to create psychological shock and resultant
disorganization in enemy forces through the employment of surprise, speed, and
superiority","The allies were losing the battle of France and were pushed to the port
of Dunkirk by the Germans. The Germans halted their push for three days which allowed
allies to organize a retreat, more than 330,000 Allied troops were rescued","In 1941,
Just before that Sunday morning, hundreds of Japanese fighter planes descended on the
base, where they managed to destroy or damage nearly 20 American naval vessels, 300
airplanes, and 2,400 Americans, Japan was hoping to cripple the American fleet before
they entered the war. The USA declared war the day after","Codenamed Operation
Overlord, the battle began on June 6, 1944, also known as D-Day, when some 156,000
American, British and Canadian forces landed on five beaches along a 50 mile stretch
of the heavily fortified coast of France’s Normandy region, creating a second
front.D-Day was known as the beginning of the end of the War.","The liberation of
Paris occured in 1944 where French Liberation forces staged an uprising against the
Germans while the Americans were quickly approaching. Once the American army arrived
in the city the German Garrison commander Dietrich von Choltitz, surrendered to the
French allowing them to have the city again","The battle of Berlin occured in 1945
and was one of the last major offensives in the war. The Soviets successfully
encircled the city, The germans refused to surrender, but the Soviets were too
overpowering for the remaining troops. Hitler ended up commiting suicide and the
remaining German troops were captured or killed. The Nazis officially surendered a
week later on May 9th"]
User = input(("Keyword or date "))
Dates = ["1939","1940","1941","1942","1943","1944","1945"]
Keywords =["Stalingrad","Blitz","Dunkirk","Pearl Harbor","D-Day","Paris","Berlin"]
#need "iteration" and "selection from list"
for D in Dates:
for K in Keywords:
if D == User:
print(facts[Dates.index(D)])
elif K == User:
print(factsK[Keywords.index(K)])
Solution 1:[1]
You're producing a product of your two lists. What you want to do is go over them together.
What you're doing now:
alphabet = ['a', 'b', 'c', 'd']
numerals = [1, 2, 3, 4]
for a in alphabet:
for n in numerals:
print(a, n)
# a 1
# a 2
# ...
# b 1
# b 2
# ...
What you want to do is go over both lists, either individually, or in a pair.
for a, n in zip(alphabet, numerals):
print(a, n)
# a 1
# b 2
# c 3
# d 4
In your case, it doesn't seem like it's particularly important to do both in the same loop. You could also just iterate over each list independently.
for D in Dates:
if D == User:
print(...)
for K in Keywords:
if K == User:
print(...)
Though, you could also just use the in
operator to check for membership.
if User in Dates:
print(factsK[Dates.index(User)])
elif User in Keywords:
print(factsK[Keywords.index(User)])
else:
print(User, 'is not a date or keyword')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | sytech |