建立一个基本程序,让用户输入一个数字来指示选项菜单的指令。一直在验证这是一个可以接受的答案?

2024-09-18 22:17:24 发布

您现在位置:Python中文网/ 问答频道 /正文

##Creation of a menu for user to choose from
print ("Please choose an activity from the list: ")
print ("1: Instruction List 1")

print "" ##Formatting line
userInput = input("Please enter your choice: ")


"""Create the desired instructions based on the user input by
integrating a function"""

def instructions_List(userInput):
    if isinstance(userInput, int) == True and userInput == 1:
       print "User instructions here"
       return
    elif userInput.isalpha():
       print "That was not an option on the menu!"
       return
    else:
       print "That was not an option on the menu!"
       return


##Run the function to do what is intended for the program

instructions_List(userInput)

因此,我们的想法是向用户显示一个选项菜单,供用户选择。一旦他们选择了一个选项,屏幕将显示菜单选项的操作说明。我在练习我的技能,首先要确保用户不能输入超出菜单显示范围的内容。我知道我最终需要集成循环,让它从头开始。但现在我要确保它只接受整数或浮点数,只要不是字符串或菜单上的选项。当我运行代码并输入一个字符串(如abc)时,试图将该字符串声明为代码中的变量的错误就会被排除。我做错什么了?你知道吗


Tags: the字符串用户anforreturnon选项