Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax Error
#1
Hey all. Just started Fundamentals of Programming 1 at my local community college. I'm on my first homework assignment which is as follows:

Land Calculation.
One acre of land is equivalent to 43,560 square feet. Write a program that asks the user to enter the total square feet in a tract of land and calculates the number of acres in the tract. (Hint: Divide the amount entered by 43,560 to get the number of acres.

I'm getting a syntax error on my variable landSize. No matter what I change the name to it says there's a syntax error. Any help would be appreciated. Thank you.

# Brian Sullivan
# Program - Complete
# This program will calculate the number of acres in a tract of
# land after the user inputs the total square feet of their tract.

# Sets the variable equal to the amount of square feet in one acre.
oneAcre = 43560

# This gets the number of square feet in the user's tract of land.
squareFeet = int(input( "How many square feet is your tract of land? ")

# This calculates the land size in acres.
landSize = squareFeet / oneAcre

# Displays the result to the user.
print( "Number of acres: " + format(landSize, ".2f") )
Reply
#2
Put your code in python code tags.

You have missed a close bracket in the input() line.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Jun-06-2017, 03:27 AM)wavic Wrote: Put your code in python code tags.

You have missed a close bracket in the input() line.

I apologize for not posting correctly. I'll make sure it doesn't happen again. I swapped to my other laptop and re-wrote the program from scratch and it worked fine. I found out that it was, in fact, the 2nd close bracket on the input line. I have a question, although it might be too advanced for my current subject material. When I run the program, I'm only allowed to input numbers without commas, such as 90000. 90,000 gives a syntax error. How would I go about fixing that?
Reply
#4
Quote: How would I go about fixing that?

replace the comma's as follows:
snum = input()
num = int(snum.replace(',', ''))
Reply
#5
(Jun-06-2017, 04:24 AM)Larz60+ Wrote:
Quote: How would I go about fixing that?

replace the comma's as follows:
snum = input()
num = int(snum.replace(',', ''))

I appreciate it. Not part of the first couple chapters we've studied so I probably shouldn't add it into the program.
Reply
#6
If you are using Python 3.6 you can type _ instead of commas. There is no need of str.replace() then
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
Seems like a bit of an obscure solution, as I don't know anyone who would write out ten thousand as 10_000   Dodgy

Remember that an integer is a whole number, so if your user enters '10000.5' you will also receive a ValueError. To avoid this, you could use float(input()) rather than int(input()) , this will allow you to accept both whole numbers and decimal numbers.

As to the comma, you will no doubt be taught how to handle errors using 'try/except' clauses in future lessons.

One final thing, this
print( "Number of acres: " + format(landSize, ".2f") )
would be better written as:
print("Number of acres: {:.2f}".format(landSize))
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
#8
(Jun-06-2017, 01:35 PM)sparkz_alot Wrote: Seems like a bit of an obscure solution, as I don't know anyone who would write out ten thousand as 10_000   Dodgy

Remember that an integer is a whole number, so if your user enters '10000.5' you will also receive a ValueError. To avoid this, you could use float(input()) rather than int(input()) , this will allow you to accept both whole numbers and decimal numbers.

As to the comma, you will no doubt be taught how to handle errors using 'try/except' clauses in future lessons.

One final thing, this
print( "Number of acres: " + format(landSize, ".2f") )
would be better written as:
print("Number of acres: {:.2f}".format(landSize))

Thank you for the suggestion! I don't remember { being discussed, but if it was and I overlooked it I'll consider the change. This is our first out of 3 programs due this week. I don't want to be adding things that aren't in the current covered material so that I'm not accused of plagiarism or anything.

I took both suggestions into consideration and made the changes. Float() does allow the user to input a decimal number, but the output doesn't change. For example, if I input "43560" I get "1" as expected. If I enter 43560.5, the output is still "1." Now, if I enter a whole number that is not exactly one acre, say "90000," I get 2.07. What's the reasoning behind this?
Reply
#9
No one will blame you if you want to learn. You don't have to wait for some lessons in school to teach you programming.

I didn't know that format() could work out of the str class :)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
You could also try parsing each individual character, and ignore non-numbers.

>>> x = '4234,093.23'
>>> def try_int(ch):
...   try:
...     _ = int(ch)
...     return True
...   except ValueError:
...     pass
...   return False
...
>>> ''.join(filter(try_int, x))
'423409323'
>>> # the decimal is included, so instead, we should split that out ahead of time...
...
>>> left, *_ = x.split(".")
>>> left
'4234,093'
>>> ''.join(filter(try_int, left))
'4234093'
Using filter() would probably be a pretty good hint that you got outside assistance, though :p
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked School project -- need help with it it says syntax error XPGKNIGHT 6 3,308 Aug-11-2022, 08:43 PM
Last Post: deanhystad
  I'm getting a syntax error very early on in my code and I can't quite figure it out. liloliveoil 1 1,996 Oct-30-2020, 05:03 AM
Last Post: deanhystad
  Unspecified syntax error hhydration 1 1,999 Oct-25-2020, 10:45 AM
Last Post: ibreeden
  Annuity function for school - syntax error peterp 2 1,966 Oct-12-2020, 10:34 PM
Last Post: jefsummers
  Invalid syntax error, where? tucktuck9 2 3,415 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  Raise an exception for syntax error sbabu 8 3,137 Feb-10-2020, 01:57 AM
Last Post: sbabu
  syntax error: can't assign to operator liam 3 4,035 Jan-25-2020, 03:40 PM
Last Post: jefsummers
  Self taught , (creating a quiz) syntax error on my array DarkAlchemyXEX 9 4,237 Jan-10-2020, 02:30 AM
Last Post: ichabod801
  If Statements and Comparisons returns a syntax error DarkAlchemyXEX 2 2,429 Jan-02-2020, 01:25 PM
Last Post: DarkAlchemyXEX
  Syntax Error: Invalid Syntax in a while loop sydney 1 4,068 Oct-19-2019, 01:40 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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