Python用户选择函数
我正在尝试制作一个具有显示五种不同标志的函数的程序。用户从列表中选择这些标志。我最大的问题是,每一面旗帜都会打印出来,而不管我选择的是哪面旗帜 我曾尝试将代码的每个部分分离成自己的函数,并使用if、elif和else解决方案来限制打印哪个标志,但没有找到解决循环问题的方法。我曾尝试将if、elif、else代码直接插入到problem函数中,但没有发现这有什么用处。我还尝试在每个if语句之后放置一个break语句,以便在进行选择后结束循环,但这没有起到任何作用 我做错了什么?循环是我最薄弱的环节,紧随其后的是if语句,我怀疑falut可能存在于if语句中,但我不确定。任何帮助都将不胜感激,谢谢 这是我的代码: def main():Python用户选择函数,python,loops,printing,selection,statements,Python,Loops,Printing,Selection,Statements,我正在尝试制作一个具有显示五种不同标志的函数的程序。用户从列表中选择这些标志。我最大的问题是,每一面旗帜都会打印出来,而不管我选择的是哪面旗帜 我曾尝试将代码的每个部分分离成自己的函数,并使用if、elif和else解决方案来限制打印哪个标志,但没有找到解决循环问题的方法。我曾尝试将if、elif、else代码直接插入到problem函数中,但没有发现这有什么用处。我还尝试在每个if语句之后放置一个break语句,以便在进行选择后结束循环,但这没有起到任何作用 我做错了什么?循环是我最薄弱的环节
intro()
选择=问题()
处理(选择)
美国()
意大利()
德国()
def intro():
打印(“绘制国旗的程序”)
打印()
def处理(选项):
对于f选项:
如果选项==“1”:
美国()
打破
elif选项==“2”:
意大利()
打破
elif选项==“3”
德国()
打破
返回美国()、意大利()、德国())
定义问题():
尽管如此:
选项=()
打印(“请选择一个标志:”)
打印(“1,美国;”)
打印(“2,意大利;”)
打印(“3,代表德国;”)
选择=输入(“-->”,)
如果选项[0]>=“1”和选项[0]您似乎有两个问题:
返回processing()
函数中的所有标志
执行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处理(选项):
对于f选项:
如果选项==“1”:
美国()
elif选项==“2”:
意大利()
elif选项==“3”:
德国()
定义问题():
尽管如此:
选项=()
打印(“请选择一个标志:”)
打印(“1,美国;”)
打印(“2,意大利;”)
打印(“3,代表德国;”)
选择=输入(“-->”,)
如果选项[0]>=“1”和选项[0]在processing()中返回所有标志,我想这是您的问题。是的,您不需要从processing()
函数返回任何内容。只需去掉整个返回行。为什么不把函数放在字典中funcs={'3':germany,…}
?然后就是funcs[choice]()
。感谢您的回复,我已经删除了处理返回,但它并没有解决打印每个标志的问题。当我选择德国国旗时,它会打印,然后所有的国旗都会打印。感谢您为我的问题的解决方案迈出了一步,在玩了一点代码之后,我看到我在我的def main()后面调用国旗:line,在我的def intro()上面:现在我的问题变成了如果移动函数调用,我收到def intro()的缩进错误:如果我移动def main,问题会自动重复。离找到解决方案又近了一步,谢谢。@Michael,def main():
行在哪里?def main():在函数调用之上,在def intro()之上。@Michael现在我很好奇,你能不能也发布你的标志函数?缩进错误其实并不难纠正。代码看起来还不错,我会发布的。我还刚刚删除了def main()下的标志函数:并发现这解决了我的问题。。。我要重新启动我的电脑,看看它是否能解决。
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