Python 用户选择的函数

Python user selected function

我正在尝试制作一个程序,其中包含显示五个不同标志的函数。用户从列表中选择这些标志。我最大的问题是无论我选择哪个标志,每个标志都会打印。

我试图将代码的每一部分都分离到它自己的函数中,并使用 if、elif 和 else 解决方案来限制打印哪些标志,但并没有找到解决似乎出现的问题的解决方案是一个循环的问题。我曾尝试将处理函数中的 if、elif、else 代码直接插入到问题函数中,但没有发现这个有用。我还尝试在每个 if 语句之后放置一个 break 语句,以便在做出选择后结束循环,但这什么也没做。

我做错了什么?循环是我最弱的 link,紧随其后的是 if 语句,我怀疑错误可能在于我的 if 语句,但我不确定。任何帮助将不胜感激,谢谢。

这是我的代码:

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

我的标志函数超出了这一点。如果它们对回答我上面的问题有用,我会 post 它们。

您似乎有两个问题:

  1. 你returnprocessing()函数中的所有标志
  2. processing() 函数执行后 立即再次调用所有标志

您只需根据用户输入的选择调用每个标志函数一次。

试试这个:

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