Python 如果用户输入字符串而不是整数,如何使代码重新运行

Python 如果用户输入字符串而不是整数,如何使代码重新运行,python,validation,syntax,Python,Validation,Syntax,如果用户在“您想要多少?(1-10)>”处输入字符串,我希望try语句重新运行,而不是获取NameError以获得正确的整数输入 我必须在大学作业中使用try/except语句 这个问题是不同的,因为我不能格式化它的链接指定的管理员,我必须坚持我的教授要求的语法 cont = str("y") item_cnt = 0 # running count of number of items ordered order_total = 0.0 # accumulate t

如果用户在“您想要多少?(1-10)>”处输入字符串,我希望try语句重新运行,而不是获取NameError以获得正确的整数输入

我必须在大学作业中使用try/except语句

这个问题是不同的,因为我不能格式化它的链接指定的管理员,我必须坚持我的教授要求的语法

cont = str("y") 
item_cnt = 0          # running count of number of items ordered
order_total = 0.0     # accumulate total dollars
price = 3.5           # all cookies rae $3.50 per box

# banner
print("Thank you for placing your order")
cust = input("please enter your name> ")

# validate data entry

while cont.lower() == "y":

    valid_data = False 

    # input and data validation

    while not valid_data: 
        # display cookie list
        print("please choose a flavor:")
        print("num\tflavor")
        print("1.\tSavannah")
        print("2.\tThin Mints")
        print("3.\tTagalongs")
        print()

        item = input("enter item number> ")

        if item == "1" or item == "2" or item == "3":
            valid_data = True
        else:
            print("That was not a valid choice, please try again")

    valid_data = False # reset boolean flag

    while not valid_data:


        try:
            qty = int(input("How many would you like? (1-10)> "))
        except Exception as detail:
            print("Error: ", detail)
        else:
            if qty >= 1 and qty <= 10:
                valid_data = True



        # determine totals
        item_total = qty * price

        # determine cookie name for output display
        if item == 1:
            name = "Savannah"
        elif item == 2:
            name = "Thin Mints"
        else:
            name = "Tagalongs"


    # verify inclusion of this item
    valid_data = False

    while not valid_data:

        incl = input("would you like to add this to your order (y/n)> ")
        print()

        if incl.lower() == "y":
            order_total = order_total + item_total
            item_cnt = item_cnt + 1
            valid_data = True
            print ("{} was added to your order".format(name))
        elif incl.lower() == "n":
            print("{} was not added to your order".format(name))
            valid_data = True
        else:
            print("that was not a valid response, please try again")



    cont = input("\nwould you like to add another? (y/n)> ")




print("order for {}".format(cust))
print("Total Items = {}".format(item_cnt))
print("Total Boxes = {}".format(qty))
print("Total Cost = ${}".format(item_total))
print()
print("Thank you for your order")       
cont=str(“y”)
物料_cnt=0#订购的物料数量的运行计数
订单总额=0.0#累计美元总额
价格=3.5#所有饼干每盒3.50美元
#横幅
打印(“感谢您下订单”)
cust=输入(“请输入您的姓名>”)
#验证数据输入
而cont.lower()=“y”:
有效数据=错误
#输入和数据验证
虽然不是有效的\u数据:
#显示cookie列表
打印(“请选择一种口味:”)
打印(“num\tflavor”)
打印(“1.\tSavannah”)
打印(“2.\t薄荷糖”)
打印(“3.\tTagalongs”)
打印()
项目=输入(“输入项目编号>”)
如果项目==“1”或项目==“2”或项目==“3”:
有效数据=真
其他:
打印(“该选项无效,请重试”)
有效_数据=错误#重置布尔标志
虽然不是有效的\u数据:
尝试:
qty=int(输入(“您想要多少?(1-10)>”)
例外情况除外,详情如下:
打印(“错误:”,详细信息)
其他:
如果数量>=1且数量=1且数量只需移动此代码:

# determine totals
item_total = qty * price

# determine cookie name for output display
if item == 1:
    name = "Savannah"
elif item == 2:
    name = "Thin Mints"
else:
    name = "Tagalongs"
在最里面的while块之外。代码不起作用的唯一原因似乎是此行处于“while”循环中:

item_total = qty * price
如果引发异常,则不会定义“数量”。如果用户输入一个非数字,你会得到一个例外,对吗?也没有理由在循环中包含下面的内容,因为如果因为输入错误而要循环,这对您没有任何好处


有一件有趣的事情……如果你先输入一个数字10,然后输入字母或其他什么,你的代码就会工作,因为“qty”会有一个值。

一种可能的方法是:将你的代码放入接受输入的函数中,检查输入,然后在不满足条件的情况下再次调用该函数。我刚刚注意到你说你不想要“NameError”。那么就不要打印它。为此,只需将catch块中的print语句替换为“pass”。
item_total = qty * price