Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax Error with if
#1
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!
Reply
#2
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
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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!
Reply
#5
(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++.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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
Reply
#7
Note that you don't need to int() randint() or 0. Those are both already integers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
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!!
Reply
#9
@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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,187 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 388 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,608 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,231 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,318 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,260 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 896 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,854 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,365 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,978 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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