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
#1
I've only just started fiddling with python and I've been trying to write a script that asks a random times table question. It doesn't do what I expect it to as it always tells me my answer is wrong when I run the script. Clearly, I need some very basic help with Python.

Anyway, this is what I've written.
print('Let us practice our seven times tables')
table = 7
from random import randint
number = randint(2, 12)
correct = number * table
print('What is')
print(number)
print('X')
print(table)
response = input('response?')
print('You said ' + response)
if response == correct:
    print('Well done')
else:
    print('Actually it is')
    print(correct)
I've really tried but I can't see why it always tells me I have it wrong when I put the correct answer in. I figure I must have made a mistake somewhere in the if / else part.

I'm using IDLE running python 3.5. My computer is running xUbuntu.


Many thanks in advance.
Reply
#2
input returns a str, the code is comparing the input str value to a number, the str will need converting to a number by using int.
if int(response) == correct:
Reply
#3
input() returns a string, and your correct variable is a number, so they can't be equal. You have to use convert the user input to a number (possibly using int()).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
The input function returns a string, so the response variable is a string value. You then compare it to the integer value of correct. They will never be equal. You want to convert the string to an integer with int:

response = int(input('response? '))
Note you can do:

print('What is', number, 'X', table)
to print it all on one line. You might also want to look into the format method of strings.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks folks. I can see why it wouldn't like that. I hadn't realised it treated every input as a string but now it makes sense. I've also taken the advice on how to combine the other bits and it's looking much tidier now.

print('Let us practice our seven times tables')
table = 7
from random import randint
number = randint(2, 12)
correct = number * table
response = input('What\'s ' + str(number) + ' X ' + str(table) + ' = ')
if int(response) == correct:
   print('Well done. You\'re spot on.')
else:
   print('Actually it is')
   print(correct)
Next step will be to get to grips with how to make it repeat and possibly add a scoring element and the ability to change the times table you're practicing.

Many thanks for the help.
Reply
#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
#7
(Dec-03-2016, 12:39 AM)sparkz_alot Wrote: 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)

Thanks. I get the points about the import and use of Double Quotes. I have to be honest and say I haven't understood the line for the imputing the response variable yet. I get the bit about treating them as intergers in the first place but I'm not sure how you have built it up, specifically: ("What's {} X {} = ".format(number, table)))

Thanks for the advice by the way.

(Dec-01-2016, 11:18 PM)ichabod801 Wrote: The input function returns a string, so the response variable is a string value. You then compare it to the integer value of correct. They will never be equal. You want to convert the string to an integer with int:

response = int(input('response? '))
Note you can do:

print('What is', number, 'X', table)
to print it all on one line. You might also want to look into the format method of strings.
Sounds like I need to look into the 'Format Method of String' as that means nothing to me at the minute.
Reply


Forum Jump:

User Panel Messages

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