+ 1
Problem with a code I made
Sorry, because I'm new It's a simple check for 1+1. a = 2 b = input() If b == a: print('Correct') Else: print('incorrect') It prompts user input, then when I type 2, It comes up with a syntax error underlining a, under the b == a line. I'm thinking maybe I have to use != instead, but syntax error comes up again. Help me please and thank you
9 Antworten
+ 8
Yeah because '2' !== 2
b = int(input()) # convert input to integer
+ 4
I too assumed there shoudn't be any errors. I pasted it in python repl it said indentation error.
You have capital I in if lowercase it, error will be gone.
+ 3
Since you are only testing one thing (b == a) for truth, you don't need to use 'elif' but can just use 'else', which runs the second code block if the thing is false (i.e. b != a), so
if b == a:
print('correct!')
elif b != a:
print('incorrect!')
can simply be
if b == a:
print('correct!')
else:
print('incorrect!')
No biggie, just a little bit simpler ;)
you can add the following line to your code and run it:
import this
+ 2
I don't see any errors over there except that "Else" should be "else" otherwise the code should work perfectly!
+ 2
Perryech Zx import makes another Python program available to your code. E. g. import math makes the trig functions available. See https://docs.python.org/3/library/
https://www.w3schools.com/python/python_modules.asp and https://www.alanzucconi.com/2015/10/29/the-top-5-easter-eggs-in-python/ 😉
+ 1
David Ashton wow thanks for that tip! as a beginner I never really understood what people meant by tidy code, that made me understand a little more.
what does the import function do? im confused with how 'import this' put all those words into the output.
0
thank you! but now that i did that, the code works but now if i input 2, it says incorrect
0
oh wow, thank you so much it works now. what i tried before was input(int() i guess that doesnt work!
0
I thought of pointing about to check for indentation.
Anyways, glad your problem's solved.