I've just started to learn python and can't get this code to work properly.
def percentage():
while True:
try:
x = int(input('First number:'))
y = int(input('Second number:'))
fraction = x/y
percentage = fraction * 100
final = "%d" % percentage
return final
except ZeroDivisionError:
pass
except ValueError:
pass
if percentage() <= 1:
print('E')
elif percentage() >= 99:
print('F')
else:
print(percentage(), "%")
The 'F' is printed only when I input x = 999 and y = 1000. Any other values just prompt the user again and again. The code should output 'E' when the percentage function returns <= 1, 'F' when it returns >= 99 and the actual percentage number when any other value is returned.
finala string that you then try to compare with 1.strbut then you try to compare it to a number. Instead, you probably want to just returnpercentage. After fixing that you will still not have the desired functionality because every time your code gets to a point where it readspercentage(), it will call the function again. Instead you want to save the result of callingpercentage()in a variable by doingmy_variable = percentage()and then comparing and printing just withmy_variable