Python Forum
My first go at Python hasn't gone well - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: My first go at Python hasn't gone well (/thread-1076.html)



My first go at Python hasn't gone well - whythedevilnot - Dec-01-2016

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.


RE: My first go at Python hasn't gone well - Yoriz - Dec-01-2016

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:



RE: My first go at Python hasn't gone well - Ofnuts - Dec-01-2016

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()).


RE: My first go at Python hasn't gone well - ichabod801 - Dec-01-2016

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.


RE: My first go at Python hasn't gone well - whythedevilnot - Dec-02-2016

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.


RE: My first go at Python hasn't gone well - sparkz_alot - Dec-03-2016

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)



RE: My first go at Python hasn't gone well - whythedevilnot - Dec-05-2016

(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.