У меня есть меню и несколько функций для продолжения работы. Моя задача - добавить дополнительные функции. Код меню прямо сейчас выглядит так.Python, подключить меню к функциям
def menu():
"""
Display the menu with the options that The Goddamn Batman can do.
"""
print(chr(27) + "[2J" + chr(27) + "[;H")
print(meImage())
print("Hi, I'm The Goddamn Batman. There is nothing I can't do. What can I do you for?")
print("1) Present yourself to The Goddamn Batman.")
print("2) Tell me your age. I'm going to show you a neat trick.")
print("q) Quit.")
проблема есть печать2. Я получаю альтернативу, чтобы появляться в Cygwin, но я, когда нажимаю «2», получаю сообщение об ошибке. Функция (не полная) выглядит следующим образом:
def Age():
"""
Ask the user's age and calculate for how many seconds the user has lived
"""
age = input("How old are you? ")
if choice == int:
print ("Ah, %s, that is old." % age)
result = (input * (int)31 556 926)
return result
print("You know what? Your age actually makes ",result, "seconds.")
else:
print("That is not a valid choice. Please write a number.")
Предыдущие функции, печать 1 (имя) и печати (д завершите работу все один), работает отлично. Например, печать 1 выглядит так:
def myNameIs():
"""
Read the users name and say hello to The Goddamn Batman.
"""
name = input("What is your name? ")
print("\nThe Goddamn Batman says:\n")
print("Hello %s - you're something special." % name)
print("What can I do for you?")
Почему печать 2 (возраст) не отвечает? Другие работают нормально, но я просто не могу добавить больше функций и заставить их работать. Надеюсь, это поможет другим людям научиться правильно подключать функции к меню. Я просто застрял.
Итак, чтобы уточнить, моя проблема в том, что функция def myNameIs():
отвечает как опция меню в Cygwin. Функция def Age():
нет. Я не знаю почему.
Заранее спасибо, я очень благодарен за любую помощь, которую вы можете предоставить.
EDIT:
ОК, после запроса. Это ВСЕ код. Мне очень жаль, что я не предлагаю просто скрипку, но я не знаю никаких скриптов, которые я могу использовать для python. Я только нахожу jsfiddle, и, похоже, в нем ничего не было для python.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Marvin with a simple menu to start up with.
Marvin doesnt do anything, just presents a menu with some choices.
You should add functinoality to Marvin.
"""
def meImage():
"""
Store my ascii image in a separat variabel as a raw string
"""
return r"""
_==/ i i \==_
/XX/ |\___/| \XX\
/XXXX\ |XXXXX| /XXXX\
|XXXXXX\_ _XXXXXXX_ _/XXXXXX|
XXXXXXXXXXXxxxxxxxXXXXXXXXXXXxxxxxxxXXXXXXXXXXX
|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
XXXXXX/^^^^"\XXXXXXXXXXXXXXXXXXXXX/^^^^^\XXXXXX
|XXX| \XXX/^^\XXXXX/^^\XXX/ |XXX|
\XX\ \X/ \XXX/ \X/ /XX/
"\ " \X/ " /"
"""
def menu():
"""
Display the menu with the options that The Goddamn Batman can do.
"""
print(chr(27) + "[2J" + chr(27) + "[;H")
print(meImage())
print("Hi, I'm The Goddamn Batman. There is nothing I can't do. What can I do for you?")
print("1) Present yourself to The Goddamn Batman.")
print("2) Tell me your age. I'm going to show you a neat trick.")
print("q) Quit.")
def myNameIs():
"""
Read the users name and say hello to The Goddamn Batman.
"""
name = input("What is your name? ")
print("\nThe Goddamn Batman says:\n")
print("Hello %s - you're something special." % name)
print("What can I do for you?")
def Age():
"""
Ask the user's age and calculate for how many seconds the user has lived
"""
age = input("How old are you? ")
if choice == int:
print ("Ah, %s, that is old." % age)
result = (input * (int)31 556 926)
return result
print("You know what? Your age actually makes ",result, "seconds.")
else:
print("That is not a valid choice. Please write a number.")
def main():
"""
This is the main method, I call it main by convention.
Its an eternal loop, until q is pressed.
It should check the choice done by the user and call a appropriate
function.
"""
while True:
menu()
choice = input("--> ")
if choice == "q":
print("Bye, bye - and welcome back anytime!")
return
elif choice == "1":
myNameIs()
else:
print("That is not a valid choice. You can only choose from the menu.")
input("\nPress enter to continue...")
if __name__ == "__main__":
main()
Что вы имеете в виду «не будет работать»? Как вы называете эти функции? –
Точно. Я не вижу, как я называю функции, это моя проблема. Def myNameIs(): был написан моим учителем. Я не вижу, как он подключен к меню. Когда я называю это в Cygwin, он работает. Второй, def Age(): был написан мной. Это просто не сработает. Меню не позволяет мне нажимать «2» в качестве допустимого параметра, «так как я должен выбрать из меню». – MrBlubbintosh
Я не знаю, как я могу быть более конкретным Кевином Дж. Чейзом. Моя проблема в том, что функция def myNameIs(): отвечает как опция меню в Cygwin. Функция def Age(): нет. Я не знаю почему. – MrBlubbintosh