I am trying to get the user input in python - python-3.x

This is my code but I am getting an error while compiling in python.
Code
print ("Enter the height :")
feet = int(input("feet:"))
Error
Enter the height :
Traceback (most recent call last):
File "main.py", line 2, in <module>
feet = int(raw_input("feet:"))
NameError: name 'raw_input' is not defined

raw_input is no longer a part of python 3.x
You can use
feet = int(input("Enter height in feet: "))
print(feet)

Related

Python: import calendar error gives unexpected result

Some other things are asked which is not in my code after execution
import calendar
y = int(input("Input the year : "))
m = int(input("Input the month : "))
print(calendar.month(y, m))
Filename I write it as calen.py
E:\pyprogs>python calen.py
Enter a string: df
fd
Traceback (most recent call last):
File "calen.py", line 1, in <module>
import calendar
File "F:\Anaconda\lib\calendar.py", line 10, in <module>
import locale as _locale
File "F:\Anaconda\lib\locale.py", line 180, in <module>
_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?'
AttributeError: module 're' has no attribute 'compile'
I expected the calendar to be printed.

How to get a useful exception message from decimal in python 3?

With Python 2, creating a Decimal with an invalid string produces a useful error message:
>>> import decimal
>>> decimal.Decimal('spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 547, in __new__
"Invalid literal for Decimal: %r" % value)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: 'spam'
While Python 3 produces a not-so-helpful message:
>>> import decimal
>>> decimal.Decimal('spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
Is there any way to get a useful message like "Invalid literal for Decimal: 'spam'" from the exception in Python 3?
I'm using Python 2.7.15 and Python 3.7.2, both on darwin.
Addenda:
It looks like Python 2 once had a not-very-helpful message for decimal.InvalidOperation: https://bugs.python.org/issue1770009
This situation looks analogous but most of it goes over my head: https://bugs.python.org/issue21227
You could monkey-patch the decimal module.
import decimal
def safe_decimal(something):
try:
funct_holder(something)
except Exception as e:
new_errror = Exception("Hey silly that's not a decimal, what should I do with this? {}".format(something))
raise new_errror from None
funct_holder = decimal.Decimal
decimal.Decimal = safe_decimal
Then you could use the monkey patched version as so
>>> decimal.Decimal('hello')
Traceback (most recent call last):
File "<input>", line 12, in <module>
File "<input>", line 6, in safe_decimal
Exception: Hey silly that's not a decimal, what should I do with this? hello

The following python 3.6.x code fails on Atom but not on IDLE

DISCLAIMER: I'm a new Python programmer (a few hours into a beginner course)
I created a slicer example on Python's 3.6.3 IDLE and it ran as supposed to. Here it is:
*email = input("What's your email address ? ").strip()
user = email[:email.find("#")]
user = user.capitalize()
domain = email[email.find("#") + 1:]
domain = domain.capitalize()
output = "Your username is {} and your domain is {}".format(user,domain)
print(output)*
However, when attempting to run it in Atom, the script-runner package gives me the following error:
*What's your email address ? user1#gmail.com
Traceback (most recent call last):
File "/Users/**user**/Desktop/Scripts/MyPyScripts/slicer.py", line 9, in <module>
email = input("What's your email address ? ").strip()
File "<string>", line 1
user1#gmail.com*
^
SyntaxError: invalid syntax
Exited with status 1 after 10.893 seconds
Would anyone have an idea? Thanks kindly!
It looks like Atom is using Python 2.x, not 3.x. input on Python 2 evaluates the string entered, e.g. the string 2+3 returns 5. Use raw_input on Python 2 to just read a string without evaluation.
>>> input('enter email: ')
enter email: test#gmail.com
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
test#gmail.com
^
SyntaxError: invalid syntax
>>> raw_input('enter email: ')
enter email: test#gmail.com
'test#gmail.com'

head first python print_lol module error

i am following head first python book using python3 version.
My directory in which i have my nester folder in
C:\Users\Nimmi\AppData\Local\Programs\Python\Python36\Lib\site-packages\nester as in my image
When i import module nester no error occurs on IDLE:
>>import nester
i have following codde written in my nester.py:
def print_lol(the_list,level):
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item)
else:
for tab_stop in range(level):
print("\t", end ='')
print(each_item)
when i try to call nester.print_lol() function, it gives me error saying:
Traceback (most recent call last):
File "", line 1, in
nester.print_lol(casu)
AttributeError: module 'nester' has no attribute 'print_lol'
where casu = ['holy day', 'life meaning']
Please tell how to resolve this error

NameError in code, 'mars' is not defined

I'm getting error for line 1 not sure what the problem is
Traceback (most recent call last):
File "python", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'mars' is not defined
Here's the code:
x = int(input("what is the fourth planet in the solar system: "))
if x == mars:
print ("correct")
else print ("incorrect")
You forgot the quotes around mars just like this 'mars'

Resources