Python Forum
My first go at Python hasn't gone well
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My first go at Python hasn't gone well
#6
As was pointed out, you were trying to compare numbers with strings. Since all your variables are numbers, except 1 (response) would it not make more sense to change just that one variable to a number rather than change all the others to strings?

You should also get in the habit of putting your "import" statements at the top of your script, not a requirement, but it makes it easier for anyone (and yourself) to know where they are rather than having to scroll thru the code trying to find them.  

One final thought, if you use double quotes ( " ), you won't have to escape the single quote ( ' ) all the time and if you use the 'format', you can eliminate all the commas, slashes, plus signs, etc.

Taking all the suggestions given above, you code would look like this:

from random import randint    # Let's move this to the top of the script

print("Let us practice our seven times tables")
table = 7
number = randint(2, 12)
correct = number * table
response = int(input("What's {} X {} = ".format(number, table)))    # Let's make this a number as well
if response == correct:
    print("Well done. You're spot on.")
else:
    print("Actually it is")
    print(correct)
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


Messages In This Thread
RE: My first go at Python hasn't gone well - by sparkz_alot - Dec-03-2016, 12:39 AM

Forum Jump:

User Panel Messages

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