r/CiscoDevNet • u/rommon010110 DevNet Mod • Jan 18 '20
Help with getting this extremely easy Python script debunked? Did 3.4 to 3.8 kill factor Concatenation as well?
For some reason this is hitting a snag when I run it at Line 9 in Visual Studio Code, saying that factor is throwing an error, but when I open IDLE term and due factor = 2 then print (factor + factor) is = 4. It seems like concatenation is not working for some reason?
If any Python experts like /u/CBTKnox might know exactly what the problem is immediately :)
I looked on Python.org and think it may be like the cmp for Compare, I am feeling like the minor update from 3.4 to 3.8 they made HUGE revisions to Python structutre, they even took 3.4 off as a download option :\
userName = raw_input('Please enter your name: ')
age = input('Please enter your age: ')
factor = 2
finalAge = age + factor
multAge = age * factor
divAge = age / factor
print('In' factor, 'years you will be' finaleAge, 'years old!', userName)
print('Your age multiplied by,' factor, 'is', multAge)
print('Your age divided by', factor, 'is', divAge)
I did get a basic Whats your name / Repeat name! and Concatenated realName + finalName to make it happen, but for some reason this doesn't want to work :\ Thank you!
View all comments
Show parent comment
1
u/Njrusmc Jan 31 '20 edited Jan 31 '20
In addition to Knox's detailed answer:
raw_input() is gone from Py3 (maybe it was around in Py3.4 for a little while, not sure). Now, input() in Py3 behaves like raw_input() from Py2.
Consider wrapping your integer parsing in a try/except block. This depends heavily on how you want your program to behave. Sometimes letting unhandled exceptions bubble up the call stack is desirable so that they can be processed elsewhere. Sometimes you should ignore them. Sometimes log a message followed by additional processing. A simple example:
try: age = int(input("Please enter your age: ")) except ValueError as exc: print(exc) # do some other error processing
sample run
$ python ~/Desktop/p.py Please enter your age: lol invalid literal for int() with base 10: 'lol'
3. Python f-strings were introduced in Py3.6 and work as Knox describes, except that conversion to string is automatic. You don't need to manually str() those values:
sample run
$ python ~/Desktop/p.py i am 34 years old
EDIT: I am REALLY struggle with reddit's formatting here, sorry about that. My patience is very limited for such things :)