2013-08-15 3 views
1
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

Не специалист по Python, но в этой ошибке четко указано, что вы не можете использовать строку в математическом вычислении. Измените переменные на числовой тип или преобразуйте их в числовой тип. –

ответ

0

Когда вы делаете "печать" заявление сделать:

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

Кроме того, вы можете использовать формат:

print("your percentage is: {0} ".format(percentage)) 

Лично рекомендую использовать формат.

6
percentage=(float(mark)*100/float(total)) 

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

Спасибо, это очень помогло! – user2685621

3

Входной возвращает строку, Вы пытаетесь сделать «30» * 100/общее, Струны не могут быть математически рассчитаны, попробуйте Int (знак) и Int (общее), а затем сделать математику.

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") 

 Смежные вопросы

  • Нет связанных вопросов^_^