Get user input as int or str

时光怂恿深爱的人放手 提交于 2019-12-02 05:13:47

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"
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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!