Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:53:49+00:00 2026-05-27T14:53:49+00:00

I just started programming and tried to write something but (of course) it failed.

  • 0

I just started programming and tried to write something but (of course) it failed. After I while I got to the real problem: The UnboundLocalError. So to save you from all the rubble around I stripped the code down to this:

def test():
    try:
        i1 = int(i1)
        i2 = int(i2)
    except ValueError:
        print "you failed in typing a number"

def input(): 
    i1 = raw_input('please type a number \n >')
    i2 = raw_input('please type a number \n >')

Then I wrote down:

>>>input()
please insert a number
> 3
please insert a number
> 2 
>>>test()

And then I got:

that was not a number
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in test
UnboundLocalError: local variable 'i1' referenced before assignment

How can I solve this in a Pythonic way? Or should I take a whole different way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:53:50+00:00Added an answer on May 27, 2026 at 2:53 pm

    The most standard way to do this is to give parameters to your test method :

    def test(i1, i2):
        try:
            i1 = int(i1)
            i2 = int(i2)
        except ValueError:
            print "you failed in typing a number"
    
    def input(): 
        i1 = raw_input('please type a number \n >')
        i2 = raw_input('please type a number \n >')
        test(i1, i2)   # here we call directly test() with entered "numbers"
    

    If you really want to test on the interactive prompt, you can do (as suggested in @FerdinandBeyer comment) :

    def test(i1, i2):
        try:
            i1 = int(i1)
            i2 = int(i2)
        except ValueError:
            print "you failed in typing a number"
        return i1, i2
        
    def input(): 
        i1 = raw_input('please type a number \n >')
        i2 = raw_input('please type a number \n >')
        return i1, i2
    

    And then, on prompt :

    >>>var1, var2 = input()
    please insert a number
    > 3
    please insert a number
    > 2 
    >>>test(var1, var2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started learning Groovy, got the PragProg book Programming Groovy and had a problem
I've just started using ASP, so while programming the code behind in C# I
I just started programming in Xcode and I'm trying to write an iphone application.
I'm a real noob who just started learning 3d programming and i have a
I'm just getting started with modular programming in C. I think I'm doing something
Just got started with Android programming and i got a task where im supposed
I just started programming in C++, and I've tried to create 2 classes where
I just started programming in C# and was reading about dividing your application /
I have just started programming and have made a few small applications in C
How would you explain to someone who has just started programming in Java, what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.