Reputation: 1
I have function that inputs a raw score (points earned out of 30 possible) and calculates the percentage score. I want a separate function that will print the percentage, but the following code does not work
def percentage():
global x
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, '%')
Upvotes: 0
Views: 113
Reputation: 323
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!
Upvotes: 0
Reputation: 12679
The first solution according to your comment :
When I run the printpercentage function with this code, it asks me to input the score out of 30 like the first function does but all i want it to do is print the string
percent_list=[]
def percentage():
global x
x = int(input('Please input the score out of 30'))
percent_list.append(x/30*100)
percentage()
def printpercentage():
print(x,'out of 30 gives a percentage of', "".join(map(str,percent_list)), '%')
printpercentage()
Second solution which is actually way to do this:
def percentage():
global x
x = int(input('Please input the score out of 30'))
return x/30*100
def printpercentage():
print('out of 30 gives a percentage of', percentage(), '%')
printpercentage()
Upvotes: 0
Reputation: 2061
(Cannot add comments, so I'll have to make an answer) If you're using Python 2, the answer given by @Prune will result in 0 or 100, for any input between 0 and 30. To get by this, you'll have to use the following import to force division to result in a floating point number:
from __future__ import division
This will give you a percentage when using Python 2.
Source: How can I force division to be floating point? Division keeps rounding down to 0
Upvotes: 0
Reputation: 27879
Okay, change your functions like this:
def percentage():
global x
x = int(input('Please input the score out of 30'))
return x / 30 * 100
def printpercentage():
percent = percentage()
print(x,'out of 30 gives a percentage of', percent, '%')
Upvotes: 0
Reputation: 77885
The preferred way to do this is to pass the desired value into the routine. Also, refactor this so that your functions carry out a single purpose. Any global declaration is a danger sign (a.k.a. "code smell")
# Given a raw score, return the percentage score
def compute_percentage(x):
return x/30*100
# Print the score and percentage
def print_percentage(x, percent):
print(x,'out of 30 gives a percentage of', percent, '%')
# Main program
score = int(input('Please input the score out of 30'))
print_percentage(score, compute_percentage(score))
Upvotes: 1