The homework initially left line 6 as 'if answer == number1 + number2' to show the error of trying to compare strings to integers, (ie. even if the user entered the correct number, it would still be deemed incorrect).
Once we were shown this error, we 'fixed' it by amending line 6 with the int() function as below;
But why do we turn 'answer' into an integer on line 6 and not when it's being input in line 5? We would have to keep turning it into an integer every time we use it if the code stays as below?
I keep getting caught out with this, i'd prefer to set them as soon as they're introduced to avoid any confusion...
Thanks!
Once we were shown this error, we 'fixed' it by amending line 6 with the int() function as below;
But why do we turn 'answer' into an integer on line 6 and not when it's being input in line 5? We would have to keep turning it into an integer every time we use it if the code stays as below?
I keep getting caught out with this, i'd prefer to set them as soon as they're introduced to avoid any confusion...
1 2 3 4 5 6 7 8 9 |
import random number1 = random.randint( 1 , 10 ) number2 = random.randint( 1 , 10 ) print ( 'What is ' + str (number1) + ' + ' + str (number2) + '?' ) answer = input () if int (answer) = = number1 + number2: print ( 'CORRECT!' ) else : print ( 'WRONG! The answer is ' + str (number1 + number2)) |