Get user input as int or str

后端 未结 2 1677
情歌与酒
情歌与酒 2021-01-26 19:18

I\'m very new to python and believe me, I\'ve searched endlessly for a solution to this but I just can\'t get it.

I have a csv with a list of monitoring plots. With the

相关标签:
2条回答
  • 2021-01-26 19:53
    choice = raw_input("Select a monitoring plot from the list: ")
    
    if choice == 'q':
        break
    
    plotSelect = int(choice)
    selected = dataList[plotSelect+1]
    

    Check if the user entered q and explicitly break out of the loop if they do (rather than relying on an exception being thrown). Only convert their input an int after this check.

    0 讨论(0)
  • 2021-01-26 19:56

    Convert it into an integer after you check that it's not 'q':

    try:
        response = raw_input("Select a monitoring plot from the list: ")
    
        if response == 'q':
            break
    
        selected = dataList[int(plotSelect) + 1]
    
        print 'You selected : ', selected[1]
        break
    except ValueError:
        print "Error: Please enter a number between 0 and 9"
    
    0 讨论(0)
提交回复
热议问题