2

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.

Michael
  • 33
  • 6
  • 1
    you return all flags in processing(), I guess it is your problem – mguijarr Apr 18 '15 at 14:30
  • Yeah, you don't need to return anything from your `processing()` function. Just get rid of that entire return line. – logic Apr 18 '15 at 14:33
  • Why not put the functions in a dictionary `funcs = {'3': germany, ...}`? Then it's just `funcs[choice]()`. – jonrsharpe Apr 18 '15 at 14:38
  • Thank you for the replies, I have deleted the processing return, but it has not solved the problem of printing every flag. When I select germany's flag, it prints, and then all of the flags print. – Michael Apr 18 '15 at 14:42

1 Answers1

0

You have two problems it seems:

  1. You return all the flags in the processing() function
  2. You call all the flags again right after the processing() function is executed

You 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
Michael
  • 33
  • 6
logic
  • 1,739
  • 3
  • 16
  • 22
  • Thank you for a step towards the solution to my problem, after playing with the code a bit, I see that I am calling the flags after my def main(): line, which is above my def intro(): Now my problem becomes one where if move the function calls, I receive a indent error with the def intro(): If I move the def main, the problem repeats itself. One step closer to finding the solution, thank you. – Michael Apr 18 '15 at 15:12
  • @Michael Where's the `def main():` line? – logic Apr 18 '15 at 15:15
  • def main(): is above the function calls, which are above def intro(): – Michael Apr 18 '15 at 15:23
  • @Michael Now I'm curious, could you post your flag functions as well? Indentation errors aren't really hard to fix. The code looks fine as of now. – logic Apr 18 '15 at 15:34
  • I'll post them. I have also just deleted the flag functions under def main(): and found that this solves my problem... I am going to restart my computer and see if it remains solved. – Michael Apr 18 '15 at 15:40
  • @Michael Ok :) The only reason I moved your main() function below all the other functions is because you didn't initially have a main() function in your question. See my edit. – logic Apr 18 '15 at 15:44