Having a hard time getting this to work.

options = [
    'option 1',
    'option 2',
    'option 3'
    ]

for x in range(len(options)):
    Opt = str(x + 1)
    print('(' + Opt + ')', options[x])
while True:
    try:
        playerOpt = int(input('input '))
        check = 'fail'
        for x in range(len(options)):
            Opt = x + 1
            if playerOpt == Opt:
                choice = options[Opt]
                check = 'pass'
        if check == 'pass':
            break
        else:
            print('Invalid Input<')
    except ValueError:
        print('Invalid Input')

print(choice)

It's supposed to print out the options in this format:
(1) option 1
(2) option 2
(3) option 3
Then wait for input and check to make sure that input is one of the options and finally set the variable "choice" to that string.

Recommended Answers

All 3 Replies

I got it to work.

options = [
    'option 1',
    'option 2',
    'option 3'
    ]

for x in range(len(options)):
    Opt = str(x + 1)
    print('(' + Opt + ')', options[x])
check = False
while check == False:
    try:
        playerOpt = int(input('input '))
        for x in range(len(options)):
            Opt = int(x + 1)
            if playerOpt == Opt:
                choice = options[x]
                check = True
    except ValueError:
        print('Value Error')


print(choice)

Now how do I get it to print 'Invalid Input' if a number not in range of the options is put in. Right not it just repeats the line "playerOpt = int(input('input '))"

Your list contains "option 1", etc, not "1".

options = [
'option 1',
'option 2',
'option 3'
]

while True:
    playerOpt = int(input('input '))
    list_test = "option %d" % (playerOpt)
    if list_test in options:
        break  ## exit while()
    print("\n Not a valid entry\n")

I did, it worked fine with zero.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.