Python Forum

Full Version: Syntax Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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") )
Put your code in python code tags.

You have missed a close bracket in the input() line.
(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?
Quote: How would I go about fixing that?

replace the comma's as follows:
snum = input()
num = int(snum.replace(',', ''))
(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.
If you are using Python 3.6 you can type _ instead of commas. There is no need of str.replace() then
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))
(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?
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 :)
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