Python script that increases a number by a percent

So, I've been working on this project for quite a while, and I can't seem to figure this out. I've written the script for increasing a number by a percent, but I keep getting this error:

File "C:\Users\jacob\Python Scripts\Percent.py", line 11, in <module>
    value = str(num + ((num / 100) * percent))
TypeError: unsupported operand type(s) for /: 'str' and 'int'

I don't know why this is happening, but here's my code.

value = ""
print("Number increase or decrease by percent")
print("")
print("Increase or decrease:") 
input = input()
if (input == "increase"):
    print("Number:") 
    num = input()
    print("Percent:") 
    percent = input()
    value = str(num - ((num / 100) * percent))
    print(value)
if (input == "decrease"):
    print("Number:") 
    num = input()
    print("Percent:") 
    percent = input()
    value = str(num - ((num / 100) * percent))
    print(value)

Any help would be greatly appreciated, I'm still a noob.

2 answers

  • answered 2021-04-08 03:17 Tim Roberts

    if statements do not need parentheses in Python. Also, don't repeat yourself. You had identical code in both branches. And if you're doing a multiply and a divide, do them in that order to avoid loss of precision. Also, when you're increasing by a percentage, you generally want to add, not subtract.

    print("Number increase or decrease by percent")
    print("")
    print("Increase or decrease:") 
    option = input()
    print("Number:") 
    num = int(input())
    print("Percent:") 
    percent = int(input())
    if option == "increase":
        value = num + (num * percent / 100)
    if option == "decrease":
        value = num - (num * percent / 100)
    print(value)
    

  • answered 2021-04-08 03:18 Zhengtao Wu

    input() returns a string, you have to convert the input string to float, and you should not let input = input(). The code below should work:

    value = ""
    print("Number increase or decrease by percent")
    print("")
    print("Increase or decrease:") 
    option = input()
    if (option == "increase"):
        print("Number:") 
        num = float(input())
        print("Percent:") 
        percent = float(input())
        value = str(num + ((num / 100) * percent))
        print(value)
    if (option == "decrease"):
        print("Number:") 
        num = float(input())
        print("Percent:") 
        percent = float(input())
        value = str(num - ((num / 100) * percent))
        print(value)