0
def intro():
   print("This program computes payroll based on")
   print("over time rate of ", overTime, " after ", workWeek, " hours")
   name = input("Enter employees name: ")
   hours = (int(input("Enter hours worked: ")))
   rate = input("Enter rate: ")

enter image description here

4
  • 2
    Please don't post pictures of code - post the actual code. PLease read How to Ask and minimal reproducible example. Commented Sep 9, 2017 at 4:06
  • 2
    Just paste your code in, mark it and hit the { } button. Commented Sep 9, 2017 at 4:08
  • 1
    I see no methods here, and furthermore, Python doesn't have variable declarations. So what, precisely, is it that you are trying to accomplish? It would be better if you gave an example input, then output, then describe how what you are doing isn't working. If you are getting any errors, please provide the error message and stack trace. Commented Sep 9, 2017 at 4:19
  • Why is your code not working? As far as I know, python should look for a variable defined in the function (not method) run. It will not find it and it will look for the larger scope. There it should find it and run as expected. Once the variable is set as a consequence of the input, the value become local (accessible only inside the function, while outside of the function still keeping the 'Y' value. Not particularly elegant, but I don't see why it should fail (if you post the actual code as said above, we can test it). Commented Sep 9, 2017 at 5:29

1 Answer 1

1

Whereas it's considered to be a bad practice and in most cases you should avoid using global variables, you can use "global" to reach your variable inside the function.

Like this:

global name
name = input("string") 
Sign up to request clarification or add additional context in comments.

3 Comments

This will certainly be a SyntaxError
Don't do this. Just return name. Then it will be accessible from the calling code.
@jedwards Yep, I've never used such a bad code to know this for sure :). Edited the answer anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.