Edit:
my initial response about print()
variable types wasn't the key to solve the issue.
The issue is, the x
and percent
need to be instantiated in outer scope before they get referred to with global x
. The following code should work:
x=0
percent=0
def percentage():
global x
global percent
x = int(input('Please input the score out of 30'))
percent = x/30*100
def printpercentage():
print(x,'out of 30 gives a percentage of', percent, '%')
percentage()
printpercentage()
Discouragement on using global
still stands :)
initial response to the question was:
In your printing function You are mixing string with integer. While print() can do both string and integer printing, it can't resolve both at a time.
So You should do:
print(str(i),'out of 30 gives a percentage of', str(percent), '%')
additionally, if percent is not neat enough your can do round(percent)
to make it nicer.
Plus you should do global percent
in percentage()
to make printpercentage()
see the variable.
Additionally, using global
is discouraged by the community because of security and polluting namespaces reasons, so think about refactoring your code by making percentage function returning x and percentage rather than writing to global variables!