Python Forum
Homework: Invalid syntax using if statements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework: Invalid syntax using if statements
#1
So I'm doing a homework assignment where we are meant to make a program that asks the user their name, age and what computer they want to buy. Once they say what computer they want to buy it checks whether they can afford it and if they can it stops but if they can't it checks whether they are 18 or over to see if they are eligible for a loan. This is the part of the code that I keep getting an error with:
def moneycheck():
    money = int(input("How much money do you have to purchase your computer? $")
    if money >= price :
                print("Congratulations you have enough to buy your dream computer! Please come inside to complete your purchase.")
    else :
        age = int(input("I'm sorry but that is not enough to buy the computer you have chosen, how old are you? ")
        if age >= 18 :
            ("You are eligible for our loan service, please come inside to discuss the terms for us to loan you a computer.")
        else :
            ("You are not eligible for our loan service and you don't have enough money for the computer you have chosen. Please save up enough money to buy your computer and come again another day.")
The specific bit where I get the error message is where it says "if money >= price :". it says invalid syntax and then highlights the colon at the end of that line, I haven't even been able to run the code.


If you're interested in the entirety of my code, here it is:
from time import sleep

#This code defines the function that will run a banner
def title ():
    print("*******************************")
    print("******Jims Computer Store******")
    print("*******************************")
    print("")


#Make a list of all the computers with lists of all possible answers to select these computers

pclist = ["Home Basic   $900","Office       $1200","Gamer        $1500","Studio       $2200"]
hblist = "home basic"
offlist = "office"
gmrlist = "gamer"
stulist = "studio"

#Define a function that prints the list of computers and asks the user which one they would like
def pcque ():
    print("Nice to meet you, "+name.title()+", this is the list of computers we have available today:")
    totalnum = 4
    count = 0
    while count < totalnum :
        print(pclist[count])
        count = count + 1
    sleep(3)
    pcchoice = input("What computer would you like? ")
    if pcchoice.lower() == hblist:
        print("You have chosen the Home Basic computer.")
        price = 900
        moneycheck()
    elif pcchoice.lower() == offlist:
        print("You have chosen the Office computer.")
        price = 1200
        moneycheck()
    elif pcchoice.lower() == gmrlist:
        print("You have chosen the Gamer computer.")
        price = 1500
        moneycheck()
    elif pcchoice.lower() == stulist:
        print("You have chosen the Studio computer.")
        price = 2200
        moneycheck ()
    else :
        print("You have chosen an invalid answer, please try again.")
        pcque ()

#function for checking if they have enough money to buy the computer
def moneycheck():
    money = int(input("How much money do you have to purchase your computer? $")
    if money >= price :
                print("Congratulations you have enough to buy your dream computer! Please come inside to complete your purchase.")
    else :
        age = int(input("I'm sorry but that is not enough to buy the computer you have chosen, how old are you? ")
        if age >= 18 :
            ("You are eligible for our loan service, please come inside to discuss the terms for us to loan you a computer.")
        else :
            ("You are not eligible for our loan service and you don't have enough money for the computer you have chosen. Please save up enough money to buy your computer and come again another day.")

#Start printing things
title ()
sleep(1)
#Ask the user their name and what pc they would like

name = input("Hi, welcome to Jims computer store, what's your name? ")
sleep(1)
pcque ()
Also, please no hate. I've only been learning python for about a week and a half and if any of my code is wrong I would like some hints please :) but I don't want anyone to do my work for me, Thank you.
Reply
#2
You're missing a close-paren on your second line of code.
Reply
#3
Thanks, even with the closed paren it still says invalid syntax and highlights the colon though.
Reply
#4
I fixed a missing paren on line 2, which resolved the issue on line 3, then there was an issue on line 7 which I resolved by closing a paren on line 6. After that, I stopped getting syntax errors from your first set of code tags.

In the future, please make sure to include the most recent full code that reproduces the issue (your first block of code tags is nice and short, while reproducing the issue, so that's great), along with the full error message. Here is my error output on your original code, then after the first fix
Output:
$ python testit.py File "testit.py", line 3 if money >= price : ^ SyntaxError: invalid syntax $ python testit.py # after first modification File "testit.py", line 7 if age >= 18 : ^ SyntaxError: invalid syntax
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid syntax Slome 2 1,953 May-13-2022, 08:31 PM
Last Post: Slome
  print(f"{person}:") SyntaxError: invalid syntax when running it AryaIC 11 16,302 Nov-07-2020, 10:17 AM
Last Post: snippsat
  Invalid syntax error, where? tucktuck9 2 3,417 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  If Statements and Comparisons returns a syntax error DarkAlchemyXEX 2 2,431 Jan-02-2020, 01:25 PM
Last Post: DarkAlchemyXEX
  Invalid syntax defining a dictionary? ep595 6 5,078 Nov-19-2019, 08:06 PM
Last Post: ThomasL
  Syntax Error: Invalid Syntax in a while loop sydney 1 4,068 Oct-19-2019, 01:40 AM
Last Post: jefsummers
  [split] Please help with SyntaxError: invalid syntax Mason 1 2,187 Apr-28-2019, 06:58 PM
Last Post: Yoriz
  SyntaxError: Invalid syntax in a while loop ludegrae 3 14,702 Dec-18-2018, 04:12 PM
Last Post: Larz60+
  SyntaxError: invalid syntax at run .py tuxo9999 10 7,240 Aug-23-2018, 03:58 PM
Last Post: Axel_Erfurt
  invalid syntax error penlemon 4 3,817 Jan-27-2018, 02:46 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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