Python Forum
variable is not defined
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variable is not defined
#1
It keeps telling me variable "number" is not defined, where am I going wrong? I'm trying to get my program to convert a number to a roman numeral. My I need the main function to use the information from the toRoman function. 


import sys

def toRoman(number):
    if number == 1:
        print (I)
    elif number == 2:
        print(II)
    elif number == 3:
        print(III)
    elif number == 4:
        print(IV)
    elif number == 5:
        print(V)
    elif number == 6:
        print(VI)
    elif number == 7:
        print(VII)
    elif number == 8:
        print(VIII)
    elif number == 9:
        print(IX)
    elif number == 10:
        print(X)
    return number

def main():
    try:
        number = int(input("Enter a Number: "))
    except:
        print("Sorry, please enter a number between 0-10")
        sys.exit()
 
print("Results: ", toRoman(number))

main()
Reply
#2
Number is only defined in the two functions. It is not defined in the global scope. That is, if you define a variable in a function, it is not available outside the function.

If you put the print statement inside the main function (in the code block under the try statement), it will work.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks for the help. I put the print statement under the try statement, when I run the program it automatically goes to the except statement. How can I get my main function to call on the toRoman function? I need the user to enter a number and have my program convert it to a roman numeral. Sorry for the newbie questions, I'm in an intro to programming class.
Reply
#4
In addition, you need to specify the roman numeral as a string

from this:
    if number == 1:
        print(I)
to:
    if number == 1:
        print('I')    # Note the quotes
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
Thanks for the help! I figured out what I was doing wrong, I just have one more issue. When the user inputs the number to be converted to a roman numeral, when I run the program it looks like this.

Enter a Number: 3
Roman Numeral: III
3

What is causing the extra 3 to show up?



Quote:import sys

print("Integer to Roman Numeral Converter")
print()

def toRoman(number):
    if number < 0:
        sys.exit()
    elif number == 1:
        print('Roman Numeral: I')
    elif number == 2:
        print('Roman Numeral: II')
    elif number == 3:
        print('Roman Numeral: III')
    elif number == 4:
        print('Roman Numeral: IV')
    elif number == 5:
        print('Roman Numeral: V')
    elif number == 6:
        print('Roman Numeral: VI')
    elif number == 7:
        print('Roman Numeral: VII')
    elif number == 8:
        print('Roman Numeral: VIII')
    elif number == 9:
        print('Roman Numeral: IX')
    elif number == 10:
        print('Roman Numeral: X')
    elif number >= 11:
        sys.exit()
    return number

def main():
    try:
        number = int(input("Enter a Number: "))
        print(toRoman(number))
    except:
        print("Sorry, please enter a number between 0-10")

main()
Reply
#6
(Apr-29-2017, 11:08 PM)hentera Wrote: What is causing the extra 3 to show up?
dont print this. You are printing the return value of toRoman function
Quote:
print(toRoman(number))
Recommended Tutorials:
Reply
#7
You are printing converted value within your toRoman function - non-Pythonic name! - and return the value, which is printed in the main
        print(toRoman(number))
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#8
I'm kind of lost.. When I erase the print(toRoman(numbers)), the input option to "Enter a Number" shows up but It wont print the roman numeral.
Reply
#9
You have to call the function - you don't have to print the result of its execution. You removed the call - required - together with the wrapping print - parasitic (in your case)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#10
How do I call the function? This is driving me nuts >_<!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020