Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 百分比计算不起作用_Python_Calculator_Percentage - Fatal编程技术网

Python 百分比计算不起作用

Python 百分比计算不起作用,python,calculator,percentage,Python,Calculator,Percentage,当我在Python3.3.2Mac中运行这个时,它会出现这个错误 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,"%" 如何修复它?当您制作打印语句时,请执行以下

当我在Python3.3.2Mac中运行这个时,它会出现这个错误

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,"%"

如何修复它?

当您制作打印语句时,请执行以下操作:

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'
此外,您还可以使用以下格式:

print ("your percentage is: %p" % (percentage))

个人建议使用格式。

输入返回字符串,您尝试执行30*100/总计,字符串无法进行数学计算,请尝试intmark和inttotal,然后执行数学运算

print("your percentage is: {0} ".format(percentage))
percentage=(float(mark)*100/float(total))

print("Your percentage is: ",percentage,"%", sep='')

不是任何Python方面的专家,但是错误清楚地表明您不能在数学计算中使用字符串。将变量更改为数字类型,或将其转换为数字类型。感谢您的帮助!!
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")