0

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, '%')
Prune
  • 76,765
  • 14
  • 60
  • 81
alicia
  • 1
  • Don't include global and call printpercentage function from percentage function. – Rajesh Mappu Oct 10 '17 at 22:12
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Specifically, you didn't include what the function *does* produce. – Prune Oct 10 '17 at 22:13
  • Yes, also don't understand what you want precisely here. @alicia – Rajesh Mappu Oct 10 '17 at 22:15
  • When you get to a resolution, please remember to up-vote useful things and accept your favourite answer (even if you have to write it yourself), so Stack Overflow can properly archive the question. – Prune Oct 23 '17 at 22:21

5 Answers5

1

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))
Prune
  • 76,765
  • 14
  • 60
  • 81
  • Someone has twice tried to edit my answer to "upgrade" is to handle the float computation. Please stop, if that's your only rationale: the code handles the math just fine. This is Python 3, so the "normal" division operator is float, not int. If you have a stylistic difference, please offer it as a comment; I'm trying to refactor the functions while preserving some of the original code. – Prune Oct 11 '17 at 00:08
0

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, '%')
zipa
  • 27,316
  • 6
  • 40
  • 58
  • 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 – alicia Oct 10 '17 at 23:31
0

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!

simplynail
  • 313
  • 1
  • 4
  • 13
  • Your initial claim is incorrect. My answer uses the original mixture of (str, int, str, float), and it resolves just fine in Python 3.4. – Prune Oct 10 '17 at 22:45
  • OK, fair enough - You're using comma as separator for print, not '+' to concatenate the variables. So yeah, variable conversion is not an issue – simplynail Oct 11 '17 at 08:13
0

(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

natn2323
  • 1,983
  • 1
  • 13
  • 30
0

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()
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88