我当前正在尝试创建程序,但需要一些帮助。我是编码的新手,并尝试过在线查找答案。程序打开时我有一个菜单,一旦调用一个选项(有6个选项),它就需要打印一张表。到目前为止,我有
def main ():
choice = printMenu()
print (choice)
def printMenu ():
print("Astronaut Mass Allowance Calculator")
print("A: Display Program options")
print("B: Display Destinations with Mass Multipliers")
print("C: Display Weight allowances for astronauts")
print("D: Calculate Personal Mass allowances")
print("E: Calculate Average Available mass and weight")
print("X: Exit")
print("Enter A, B, C, D, E or X to proceed:")
main()
一旦输入了字母,我怎么称呼其余的?
答案 0 :(得分:3)
在您的代码printMenu
中不返回任何内容。因此choice = printMenu()
是没有用的。您应该使用input
或raw_input
(取决于python版本)来读取用户输入,然后使用
if choice == "A":
displayProgramOptions()
elif choice == "B":
// ...
else:
print("Unknown option")
或使用dict
可以将字符串映射为函数。即
funcs = {
"A": displayProgramOptions,
"B": displayDestinationsWithMassMultipliers,
// ...
}
if choice in funcs:
funcs[choice]()
else:
print("Unknown option")
答案 1 :(得分:0)
我从您的问题中了解到,您希望用户查看打印的菜单并选择一个选项。然后,根据用户的输入,您希望您的程序采取进一步的行动。如果这是您所期望的,那么我建议从“菜单选项”中删除最后的打印,并使其成为“输入”的一部分,并像这样使用它:
def main ():
printMenu()
ip = input("Enter A, B, C, D, E or X to proceed:")
#do what you want with this option
print("you entered: " + ip)
def printMenu ():
print("Astronaut Mass Allowance Calculator")
print("A: Display Program options")
print("B: Display Destinations with Mass Multipliers")
print("C: Display Weight allowances for astronauts")
print("D: Calculate Personal Mass allowances")
print("E: Calculate Average Available mass and weight")
print("X: Exit")
#Last line removed from here
main()
答案 2 :(得分:0)
为了捕获响应,您可以使用input()函数。您需要进一步的帮助吗?
def main ():
choice = printMenu()
print (choice)
# Do something here with the choice, as P.Dmitry suggests.
def printMenu ():
print("Astronaut Mass Allowance Calculator")
print("A: Display Program options")
print("B: Display Destinations with Mass Multipliers")
print("C: Display Weight allowances for astronauts")
print("D: Calculate Personal Mass allowances")
print("E: Calculate Average Available mass and weight")
print("X: Exit")
return input("Enter A, B, C, D, E or X to proceed:")
main()
答案 3 :(得分:0)
我会将逻辑分解为单独的函数,以便它们每个都可以输出自己的信息:
def A():
print('A CODE HERE')
def B():
print('B CODE HERE')
def C():
print('C CODE HERE')
def D():
print('D CODE HERE')
def E():
print('E CODE HERE')
def X():
quit()
def main():
choice = printMenu()
print('YOU CHOSE:', choice)
if choice == 'A':
A()
elif choice == 'B':
B()
elif choice == 'C':
C()
elif choice == 'D':
D()
elif choice == 'E':
E()
elif choice == 'X':
X()
def printMenu():
print("Astronaut Mass Allowance Calculator")
print("A: Display Program options")
print("B: Display Destinations with Mass Multipliers")
print("C: Display Weight allowances for astronauts")
print("D: Calculate Personal Mass allowances")
print("E: Calculate Average Available mass and weight")
print("X: Exit")
# Collect input from the user.
choice = input("Enter A, B, C, D, E or X to proceed: ")
# Return the input.
return choice.upper()
while True: # Infinitely display the choice menu
main()