我有5种东西的菜单选项。如果用户输入的数字不在1到5之间,则我的程序会再次询问该数字,但是即使用户输入的数字有效,程序仍会结束。
print(" ")
print("pick a menu option between 1-5")
print(" ")
print(" ")
print("1 - Enter RLE")
print("2 - Display ASCII art")
print("3 - covert ASCII art option")
print("4 - convert RLE option")
print("5 - Quit")
print(" ")
print(" ")
print(" ")
user=0
user=int(input('select a number between 1 and 5'))
if user == 1:
print("hi")
elif user == 2:
user = input('select a file with an ASCII art image')
f = open(user, 'r')
if f.mode == 'r':
showart = f.read()
print(showart)
# asking user for file
#showing the file
#file name LogoArt.txt
elif user == 3:
print("hi")
elif user == 4:
print("hi")
elif user == 5:
print('goodbye')
import sys
sys.exit()
#exits the program
else:
user=int(input("select a number between 1 and 5"))
我希望else的输出能够引起原始问题
答案 0 :(得分:0)
def ask (user) :
if user == 1:
print("hi")
elif user == 2:
user = input('select a file with an ASCII art image ')
f = open(user, 'r')
if f.mode == 'r':
showart = f.read()
print(showart)
elif user == 3:
print("hi")
elif user == 4:
print("hi")
elif user == 5:
print('goodbye')
import sys
sys.exit()
while (True) :
print(" ")
print("pick a menu option between 1-5 ")
print(" ")
print(" ")
print("1 - Enter RLE ")
print("2 - Display ASCII art ")
print("3 - covert ASCII art option ")
print("4 - convert RLE option ")
print("5 - Quit ")
print(" ")
print(" ")
print(" ")
user=int(input("Select an integer between 1 and 5 : "))
if (user<5 and user > 1) :
ask(user)
else:
user=int(input("Please enter a number between 1 and 5 : "))
while (user > 5 or user < 1) :
user=int(input("Please enter a number between 1 and 5 : "))
ask (user)
输出:
答案 1 :(得分:0)
这是一个很好的,简单的,无循环的答案,带有函数定义,甚至还有一些递归。如果您是python或编程的新手,那确实是一件好事。祝您好运。并随时询问有关其工作原理的任何问题。
def Menu():
#Put all your option prints here.
print(" ")
print(" ")
Option = int(input("Pick an option between 1 and 5. ")
print(" ")
if Option == 1:
pass #Replace each "pass" with what you want that Option to do.
Menu()
elif Option == 2:
pass
Menu()
elif Option == 3:
pass
Menu()
elif Option == 4:
pass
Menu()
elif Option == 5;
import sys
sys.exit()
else:
Menu()
Menu()