我在一个名为python1.py的文件中创建一个代码,它接受两个输入值并计算百分比
var1,var2 = input("Give 2 integers: student mark and then what the test was out of").split() # the requested marks are split by a white space
var1,var2 = [int(x) for x in [var1, var2]] # this converts the input values to integers
percentage = round(var1/var2*100) # calculation of the percentage truncating the fractional part
print("%d out of %d is %d%%" %(var1, var2, percentage))
在自动标记上运行时会产生以下错误: 给2个整数:学生标记,然后测试是什么
Traceback (most recent call last):
File "question1.py", line 1, in
var1,var2 = input("Give 2 integers: student mark and then what the test was out of").split() # the requested marks are split by a white space
File "", line 1
21 35
^
SyntaxError: unexpected EOF while parsing
我该怎么做才能解决错误
答案 0 :(得分:0)
使用raw_input
代替输入
var1, var2 = raw_input("Give 2 integers: student mark and then what the test was out of ").split() # the requested marks are split by a white space
代码中的百分比计算不正确。这是一个有效的代码:
var1, var2 = raw_input("Give 2 integers: student mark and then what the test was out of ").split() # the requested marks are split by a white space
print var1, var2
var1,var2 = [int(x) for x in [var1, var2]] # this converts the input values to integers
percentage = float(var1)/float(var2)*100 # calculation of the percentage truncating the fractional part
print("%d out of %d is %d%%" %(var1, var2, percentage))
Give 2 integers: student mark and then what the test was out of 10 20
10 20
10 out of 20 is 50%
答案 1 :(得分:0)
您使用的是http://pythonfiddle.com/还是其他类似的网站? 如果是,请尝试输入
"21 35"
如果var1 < var2
,var1 / var2
为0
:
percentage = round(var1 * 1.0 / var2 * 100)
print("%d out of %d is %d%%" %(var1, var2, percentage))