【问题标题】:Python user selected functionPython 用户选择的函数
【发布时间】:2015-04-18 14:24:13
【问题描述】:

我正在尝试制作一个具有显示五个不同标志的函数的程序。用户从列表中选择这些标志。我最大的问题是打印每个标志,而不考虑我选择的标志。

我尝试将代码的每个部分单独保存到自己的函数中,并使用 if、elif、else 解决方案来限制打印的标志,但没有找到解决方案来解决似乎是循环的问题。我曾尝试将处理函数中的if、elif、else代码直接插入问题函数中,但没有发现这个有用。我还尝试在每个 if 语句之后放置一个 break 语句,以便在进行选择后结束循环,但这没有任何作用。

我做错了什么?循环是我最薄弱的环节,紧随其后的是 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

我的标志功能超出了这一点。如果它们对回答我上面的问题有用,我会发布它们。

【问题讨论】:

  • 你在 processing() 中返回所有标志,我猜是你的问题
  • 是的,您不需要从 processing() 函数中返回任何内容。只需摆脱整个返回线。
  • 为什么不把函数放入字典funcs = {'3': germany, ...}?然后就是funcs[choice]()
  • 谢谢大家的回复,我已经删除了处理返回,但是并没有解决打印每个flag的问题。当我选择德国国旗时,它会打印,然后打印所有的国旗。

标签: python loops printing selection statements


【解决方案1】:

看来你有两个问题:

  1. 您返回 processing() 函数中的所有标志
  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

【讨论】:

  • 感谢您为解决我的问题迈出了一步,在玩了一点代码之后,我看到我在我的 def main(): 行之后调用了标志,它在我的 def 上方intro():现在我的问题变成了,如果移动函数调用,我会收到一个带有 def intro() 的缩进错误:如果我移动 def main,问题就会重演。离找到解决方案又近了一步,谢谢。
  • @Michael def main(): 行在哪里?
  • def main(): 在函数调用之上,在 def intro() 之上:
  • @Michael 现在我很好奇,你能发布你的标志函数吗?缩进错误并不难修复。到目前为止,代码看起来还不错。
  • 我会发布它们。我也刚刚删除了 def main(): 下的标志函数,发现这解决了我的问题...我要重新启动计算机,看看它是否仍然解决。
猜你喜欢
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
相关资源
最近更新 更多