Attacking Python
This is taken from my blog (palukingston.com) where I’m writing about my learning process as a software tester and developer. Thanks for taking a look!
February 13, 2017
About a week ago, I decided to learn python. I’ve been looking at some tutorials, getting through the basics and all. A few days in though, I hit a wall in my learning progress. After giving up, I slept on it, and decided well if one tutorial’s approach is not driving it home, then why not switch it up! I decided to look for a tutorial on building a simple app so that I could get some practical experience. I pretty quickly decided on a YouTube tutorial from 2012. I knew this could potentially lead to headaches — since the guy in the tutorial was using an older version of python, as well as a different IDE. But hey — that’ll just make it more interesting!
So — I found my first discrepancy in python syntax between versions. In the tutorial, we were writing a “random” number guessing game. There was a variable integer of 35 (randomNumber = 35) declared. The tutorial had you write the statement userGuess = input(“Your guess: “). When I ran the program this was the outcome:
Guess a number between 1 and 100.Your guess:1Traceback (most recent call last):File "C:/Users/paluk_000/PycharmProjects/SimplePythonProgram_excercise/First.py", line 24, in <module>main()File "C:/Users/paluk_000/PycharmProjects/SimplePythonProgram_excercise/First.py", line 16, in mainelif userGuess > randomNumber:TypeError: unorderable types: str() > int()
I banged my head against the wall for about 3 minutes and decided, rather than embrace my frustration, why not ask the Google oracle? One quick search and an entry on stackoverflow.com shed light on my problem. Apparently on my version of Python, you must declare the variable input as an int data type as well as put brackets around your print statement. Sweet, problem surmounted with relative ease and speed!
Ok, another discrepancy — the statement in this tutorial
print random.randint(1,10).. uses improper syntax. The correct statement ended up being this:
print(random.randint(1,10))Gotta remember those darn brackets!
Get Ben Kingston’s stories in your inbox
Join Medium for free to get updates from this writer.
Aha! Another bug:
C:\Python34\python.exe C:/Users/paluk_000/PycharmProjects/SimplePythonProgram_excercise/First.pyGuess a number between 1 and 100.Your guess:1Guess higher!nnnnnThanks for playing our game.Your guess:tTraceback (most recent call last):File "C:/Users/paluk_000/PycharmProjects/SimplePythonProgram_excercise/First.py", line 30, in <module>main()File "C:/Users/paluk_000/PycharmProjects/SimplePythonProgram_excercise/First.py", line 16, in mainuserGuess = int (input("Your guess:"))ValueError: invalid literal for int() with base 10: 't'Process finished with exit code 1
Good times!







