Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with syntax error
#1
I am just practicing with python and I keep getting invalid syntax pop up. I want it to end the script if the user says no. Please help!

print (" Hello, I will be asking you multiple questions to build a profile of who you are.")
answer = input ("Is this ok?")
if answer == ("no") 
import sys
sys.exit()
if awnser == ("yes")             
Name = input(" First of all, what is your name?")
print ("Ahh, hello " + Name )
Age = input ("Now " + Name + " What is your age? Now I must ask you enter this in numbers otherwise I will get confused")
Address = input ("What is your address?"
Reply
#2
Typically, you put all imports at the top of you script.

You want to add a colon at the end of your if statements and indent the line after the if statement, also you don't need the parenthesis:

if answer == "no":
    sys.exit()
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
I'll also add, since your practicing, it is a good idea to avoid bad habits before they become habits, so familiarize yourself with this document: Python Style Guide

Make sure your closing parenthesis matches your opening parenthesis (see your line 10).
Use lower case for variable names, ie: name instead of Name, age instead of Age and so on.
Use 4 spaces (not tabs) for indentation.
Remember that input() will return a string, if you are looking for a number use something like int(input()), so this line:
Age = input ("Now " + Name + " What is your age? Now I must ask you enter this in numbers otherwise I will get confused")
would look like this:
age = int(input("Now " + Name + " What is your age? Now I must ask you enter this in numbers otherwise I will get confused"))
and would be much neater if you use formatting:
age = int(input("Now, {} what is your age? Now I must ask you enter this in numbers otherwise I will get confused".format(name)))
The same would be true for your print functions:
print("Ahh, hello {}".format(name))
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
To elaborate on sparkz_alot:

Not sure what you are using for an IDE, but it may have something similar to PyCharm's inspect process.
This is a good tool to force yourself to use proper style, I use it on myself quite often (except at the
beginning of a project where my main goal is to get the code into the editor).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,154 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 371 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,542 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,205 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,288 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 920 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Syntax error? I don't see it KenHorse 4 1,241 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 881 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,828 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,344 May-18-2022, 06:50 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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