我对Python很新,我只是想知道最好的方法是什么(非常简单的问题):
我有一个提示用户选择选项的功能,如果他们输入的选项无效,它将再次调用该功能:
def choose_colour():
print "Select colour:"
print "1. Red"
print "2. Blue"
print "3. Green"
selection = raw_input()
最初我有一个if语句比较输入和IF它是一个不正确的输入我会再次调用该函数本身。这是不实际和错误的。
def choose_colour():
print "Select colour:"
print "1. Red"
print "2. Blue"
print "3. Green"
selection = raw_input()
if selection == "1":
colour = "Red"
elif selection == "2":
colour = "Blue"
elif selection == "3":
colour = "Green"
else:
print "Please select, 1, 2 or 3"
choose_colour()
编辑:
这有效,但我更喜欢循环解决方案。
def set_TPL_colour():
print "Enter TPL colour value:"
print "1. WHITE"
print "2. GREEN"
print "3. AMBER"
print "4. RED"
print ">>>",
option = raw_input()
if option == "1":
tpl_colour = "WHITE"
return tpl_colour
elif option == "2":
tpl_colour = "GREEN"
return tpl_colour
elif option == "3":
tpl_colour = "AMBER"
return tpl_colour
elif option == "4":
tpl_colour = "RED"
return tpl_colour
else:
print "ERROR incorrect option selection"
tpl_colour = set_TPL_colour()
return tpl_colour
答案 0 :(得分:1)
您不需要再次调用该函数,只需将其置于while循环中并在输入无效时重新分配该变量:
def choose_colour():
print "Select colour:"
print "1. Red"
print "2. Blue"
print "3. Green"
selection = raw_input()
while selection not in ("1","2","3"):
print "Please choose a valid option"
selection = raw_input()
if selection == "1":
colour = "Red"
elif selection == "2":
colour = "Blue"
elif selection == "3":
colour = "Green"
清洁版:
def choose_colour():
colourChoices = {"1":"Red", "2":"Blue", "3":"Green"}
selection = raw_input("Select colour:\n 1. Red\n 2. Blue\n 3. Green")
while !colourChoices[selection]:
print "Please enter valid data"
selection = raw_input("Select colour:\n 1. Red\n 2. Blue\n 3. Green")
colour = colourChoices[selection]
答案 1 :(得分:1)
使用字典,示例 -
def choose_colour():
print "Select colour:"
print "1. Red"
print "2. Blue"
print "3. Green"
colordict = {"1":"Red" , "2":"Blue", "3":"Green"}
selection = raw_input()
ret = colordict.get(selection)
if not ret:
return choose_colour()
return ret
你也可以做while循环 -
def choose_colour():
colordict = {"1":"Red" , "2":"Blue", "3":"Green"}
ret = None
while not ret:
print "Select colour:"
print "1. Red"
print "2. Blue"
print "3. Green"
selection = raw_input()
ret = colordict.get(selection)
return ret
示例/演示 -
>>> def choose_colour():
... print "Select colour:"
... print "1. Red"
... print "2. Blue"
... print "3. Green"
... colordict = {"1":"Red" , "2":"Blue", "3":"Green"}
... selection = raw_input()
... ret = colordict.get(selection)
... if not ret:
... return choose_colour()
... return ret
...
>>> choose_colour()
Select colour:
1. Red
2. Blue
3. Green
5
Select colour:
1. Red
2. Blue
3. Green
2
'Blue'
答案 2 :(得分:0)
像这样。
input = raw_input()
while not_valid(input):
print_options()
input = raw_input()
答案 3 :(得分:0)
您可以使用简单的方法。首先,使用我的Input()函数和控制错误。此外,你可以在这里放一个长长的列表,这个程序可以处理它。
# If your input value is only a number then use "Value.isdigit() == False".
# If you need an input that is a text, you should remove "Value.isdigit() == False".
def Input(Message):
Value = None
while Value == None or Value.isdigit() == False:
try:
Value = str(input(Message)).strip()
except InputError:
Value = None
return Value
# Your answer:
def choose_colour():
# Your colors list
Colors = ["Red", "Blue", "Green"]
# Print your menu
print("Select colour:")
for Item in Colors:
print(str(Colors.index(Item) + 1) + ". " + Item)
print("")
Choice = -1
# If user entered an incorrect answer, ask your question again
while Choice <=0 or Choice > len(Colors):
Choice = int(Input("Your choice: "))
# Print user choice
print("You choosed " + Colors[Choice - 1])
choose_colour()