【发布时间】:2020-04-28 18:10:30
【问题描述】:
我还是 Python 和编程以及 StackOverflow 的新手,请原谅我缺乏正确的编码词汇。我的问题是下面的代码。我可以很好地运行两个 while True 循环,但是一旦它到达注释 #PROBLEM STARTS HERE 下的代码,我不知道我到底做错了什么。我收到的错误消息是 unsupported operand type(s) for /: 'str' and '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))
任何帮助将不胜感激。
【问题讨论】:
-
这能回答你的问题吗? How can I read inputs as numbers?
-
你知道
discountPtg是一个字符串(因为你在赋值给check时将它转换为float),那你为什么在算术表达式中使用它呢? -
你需要做:basePrice = float(basePrice) and discountPtg = float(discountPtg)
标签: python python-3.x input floating-point int