Skip to main content
 首页 » 编程设计

python之百分比计算不起作用

2025年05月04日11sxdcgaq8080

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' 

我该如何解决?

请您参考如下方法:

percentage=(float(mark)*100/float(total)) 
 
print("Your percentage is: ",percentage,"%", sep='')