Python Forum

Full Version: Syntax Error with if
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello! This is my first ever post. I am really new to Python and programming in general. Only about a week that i try to figure out how to code.

I tried making a program that will generate a random number, then i input a number trying to guess the random one. The program tells me if my input is higher or lower than the number and i guess again until i find the correct one. Finally it notifies me that i found the correct number and also how many attempts i needed to find it.
While i m pretty sure my logic behind this is correct, i run into silly syntax errors that probably come from the "if" statement. I am using Python 3.7

I ve seen that the syntax should be

if condition:
commands
elif condition:
commands
else:
commands

while it seems simple it wont work :)
my code is the following

import random
z = int(random.randint(0,100))
att = int(0)                                        # att will be used to count the attemps inside my loop
solve = False                                       # I use solve as an exit mechanic from while loop
while solve == False:
    x = input(int('Enter your guess: ')
    if x > z 
      print('The number is smaller than the input')
      guess = att + 1
    elif x < z:
      print('The number is bigger than the input')
      guess = att + 1
    else:
      print('Congratulations! You have found the correct number!')
      print('It took you ' + int(guess) + 'attemps')
      solve = True 
The error i get is this https://i.imgur.com/5PFCcNW.png
also if i try to put the : in the first if statement i get this https://i.imgur.com/c7avGC0.png

I am trying to solve this for 3h now ruining my study and i finally decided that i need help for someone with experience!

Thank you in advance!
Here is the full working code

import random
z = int(random.randint(0,100))
att = int(0)                                        # att will be used to count the attemps inside my loop
solve = False                                       # I use solve as an exit mechanic from while loop
while (solve == False):
    x =int(input('Enter your guess: '))
    if x > z:
        print('The number is smaller than the input')
        guess = att + 1
    elif x < z:
        print('The number is bigger than the input')
        guess = att + 1
    else:
        print('Congratulations! You have found the correct number!')
        print('It took you ' + int(guess) + 'attemps')
    solve = True
You need a colon at the end of the if statement and and close parenthesis at the end of the previous line.

And what are you using to run your code? Please change to something where you can copy and paste the error instead of posting links to pictures.
Thank you soooo much both of you!
It seems that after 3h i burned out and the mistake was actually sillier than i thought.

I use the IDLE Python 3.7 to code. I ll try to figure out how to do that without screenshots. I m really new to all this!

Again thnx you guys saved the night!
(Jan-04-2019, 10:16 PM)MrNtorees Wrote: [ -> ]I use the IDLE Python 3.7 to code.

People hate IDLE around here. Popular alternatives are PyCharm, VSCode, and Notepad++.
Awesome i ll try those out.


Btw i found another error to the code that had to do with how i wrote the x variable.
it should be x = int(input('enter....')) was the other way around
Note that you don't need to int() randint() or 0. Those are both already integers.
I am so excited that i joined this community!
I feel i will improve really fast with people like you around.

So i fixed the syntax error and noticed that the code had BIG issues except for the syntax. So in order to redeem myself i present you the REAL 'Guess the number' script corrected.

import random
z = random.randint(0,100)
guess = 0
solve = False
while solve == False:
    x = int(input('Enter your guess: '))
    if x > z:
        print('The number is smaller than the input')
        guess = guess + 1
    elif x < z:
        print('The number is bigger than the input')
        guess = guess + 1
    else:
        print('Congratulations! You have found the correct number!')
        print('It took you ' + str(guess) + ' attemps')
        solve = True
I also implemented the final piece of advice in there.
To tell you the truth, i thought that it was too much the int()randint() cause second one has the int inside it but i put it there to make sure.
PS: I also removed the guess = int(0) to guess = 0 for the same reason.

So with your help we are victorious!!
@bvdinesh
man i honestly just saw your reply with the full code!
I really thank you for this, but i was actually really glad i missed it cause it made me fix the code, and i reached to that conclusion myself too!

Programming is sooo awesome what was i doing all those years!