I am trying to make a program with functions that display five different flags. The user selects these flags from a list. My largest problem is that every flag prints, without regard to the flag I chose.
I have tried to keep each part of the code seperate into its own function, and use an if, elif, else solution to limit which flag prints, but have not had much luck with finding a solution to what appears to be a problem with a loop. I have tried to insert the if, elif, else code in the processing function, into the question function directly, but did not find this to be useful. I have also tried to place a break statement after each if statement, as to end the loop once a selection was made, but this did nothing.
What have I done wrong? loops are my weakest link, followed closely by if statements, I suspect the falut may lie in my if statement, but I am unsure. Any help would be appreciated, thank you.
This is my code:
def main():
intro()
choice = question()
processing(choice)
unitedStates()
italy()
germany()
def intro():
print("program to draw a national flag.")
print()
def processing(choice):
for f in choice:
if choice == "1":
unitedStates()
break
elif choice == "2":
italy()
break
elif choice == "3"
germany()
break
return unitedStates(), italy(), germany()
def question():
while True:
choice = ()
print("Please choose a flag:")
print(" 1, for United States;")
print(" 2, for Italy;")
print(" 3, for Germany;")
choice = input("-->", )
if choice[0] >= "1" and choice[0] <= "5" and len(choice) == 1:
break
print("Choice must be, between 1-5, not ", choice + ".")
print("Try again.")
print()
print()
return choice
My flag functions are beyond this point. I'll post them if they are useful to answering my question above.
You have two problems it seems:
processing()
functionprocessing()
function is executedYou only need to call each flag function once, based on the choice inputted by the user.
Try this:
def main():
intro()
choice = question()
processing(choice)
def intro():
print("program to draw a national flag.")
print()
def processing(choice):
for f in choice:
if choice == "1":
unitedStates()
elif choice == "2":
italy()
elif choice == "3":
germany()
def question():
while True:
choice = ()
print("Please choose a flag:")
print(" 1, for United States;")
print(" 2, for Italy;")
print(" 3, for Germany;")
choice = input("-->", )
if choice[0] >= "1" and choice[0] <= "5" and len(choice) == 1:
break
print("Choice must be, between 1-5, not ", choice + ".")
print("Try again.")
print()
print()
return choice
#========================================================
# Flag functions added by Michael
#========================================================
def unitedStates():
for i in range(1,5):
print((" * * *" * 2), ("X" * 34))
print("* * * " * 2)
print(" * * *" * 2, "X" * 34)
for i in range(4):
print()
print("X" * 47)
print()
def italy():
green(14, "G")
white(14, "W")
red(14, "R")
for i in range(15):
print(green(15, "G") + white(15, ".") + red(15, "R"))
print()
def green(gn, gch):
pg = gn * gch
return pg
def white(wn, wch):
pw = wn * wch
return pw
def red(rn, rch):
pr = rn * rch
return pr
def germany():
black(47, "G")
red(47, "W")
yellow(47, "R")
for cBlack in range(5):
print(black(47, "B"))
for cRed in range(5):
print(red(47, "`"))
for cYellow in range(5):
print(yellow(47, "Y"))
def black(gn, gch):
pg = gn * gch
return pg
def red(wn, wch):
pw = wn * wch
return pw
def yellow(rn, rch):
pr = rn * rch
return pr
main() #call main here