Python Forum
variable is not defined
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variable is not defined
#11
main()
toRoman(number)
Are examples of calling functions
print(toRoman(number))
is calling function print with the result of toRoman(number) call
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
#12
When I run the program it will show me the roman numerals line twice now. I erased the print line from my main function and changed it to the toRoman function.

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: "))
toRoman(toRoman(number))
except:
print("Sorry, please enter a number between 0-10")


main()

Ah okay, I got it! Thanks for all the help!
Reply
#13
Wall really?
toRoman(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
#14
Lol sorry, I'm a newbie >_<.

One last question, with my toRoman(number) function, is returning the number at the bottom of the function required? When should I return something on a function?
Reply
#15
Functions don't have to return anything if you don't want them to. You don't use the return value, so it doesn't matter here.

What SHOULD you return? Probably the string instead of printing it directly.
Reply
#16
It's best to return at the end of the function. That's where people expect the return to be, and if they don't see a return statement there, they may assume you don't have one (which implicitly returns None). Note that as a soon as you return a result, the function stops, and control of the program goes back to the point where the function was called. So you certainly want to wait until all the necessary processing gets done before you return a value.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#17
also, your exception is misleading as the user is not given the opportunity to enter another number, rather the script just ends.
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


Forum Jump:

User Panel Messages

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