mark=input("Please enter the mark you received for the test:")
total=input("Please enter the mark the test was out of:")
percentage=(mark*100/total)
print("Your percentage is:"),percentage,"%"
当我在python 3.3.2 mac中运行它时会出现此错误。
Traceback (most recent call last):
File "/Users/user1/Desktop/Percentage2.py", line 4, in <module>
percentage=(mark/total*100)
TypeError: unsupported operand type(s) for /: 'str' and 'str'
我该如何解决?
答案 0 :(得分:6)
percentage=(float(mark)*100/float(total))
print("Your percentage is: ",percentage,"%", sep='')
答案 1 :(得分:3)
输入返回一个字符串,你试图做“30”* 100 /总,字符串不能用数学方法计算,尝试int(mark)和int(total)然后做数学运算。
try:
Mark = int(input("Please enter the mark you received for the test."))
Total = int(input("Please enter the mark the test was out of."))
Perc = (Mark*100) / Total
print("Your Percentage is"+Perc)
except:
print("Numbers not entered. Please try again")
答案 2 :(得分:0)
当您进行“打印”声明时:
print ("your percentage is: %p" % (percentage))
此外,您可以使用格式:
print("your percentage is: {0} ".format(percentage))
个人建议使用格式。