不确定我的问题是什么,输入变量无法进行[关闭]

问题描述 投票:0回答:2
我对Python和编程以及StackOverflow还是陌生的,请原谅我缺乏适当的编码vocab。我的问题是下面的代码。我可以在True循环很好的情况下运行这两个程序,但是一旦到达注释#PROBLEM STARTS HERE的代码,我就不知道我到底在做什么错。我收到的错误消息是

/不支持的操作数类型:'str'和'int'。

gameName = str(input('Enter game title: ')) while True: basePrice = input('Enter the base price: ') try: check = float(basePrice) break except ValueError: print('Please enter a valid price.') continue while True: discountPtg = input('Enter the discount(%): ') try: check = float(discountPtg) break except ValueError: print ('Please enter just a number for your discount.') #PROBLEM STARTS HERE disctdPrice = (discountPtg / 100) * basePrice savedCost = basePrice - disctdPrice print ('\nSTATS\n') print (gameName) print ('Base price: RM' + str(basePrice)) print ('Discount: ' + str(discountPtg) + '%') print ('Discounted price: ' + str(disctdPrice)) print ('You saved: ' + str(savedCost))
任何帮助将不胜感激。
python python-3.x input floating-point int
2个回答
1
投票
您检查了一下是否为数字,但从未将其转换为一个数字。在此行:

disctdPrice = (discountPtg / 100) * basePrice

DicountPtg应该变成这样的浮点数:

disctdPrice = (float(discountPtg) / 100) * basePrice


0
投票
谢谢大家。我已经通过进行以下修改来弄清楚了:

disctdPrice = (float(discountPtg)/100) * float(basePrice) savedCost = float(basePrice) - float(disctdPrice)

© www.soinside.com 2019 - 2024. All rights reserved.