473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can't Assign to Literal Syntax Error

This is my function. Can anyone help me figure it out?
Expand|Select|Wrap|Line Numbers
  1. count = 0
  2. def fizz_count(x):
  3.     for 'fizz' in fizz_count:
  4.         int(count) +=1
  5.     return count
Jun 2 '13 #1
4 3112
bvdet
2,851 Expert Mod 2GB
You cannot assign a value to a string. In your for loop, python attempts to assign a value to the target 'fizz'.
Expand|Select|Wrap|Line Numbers
  1. >>> 'fizz' = 0
  2. Traceback (  File "<interactive input>", line 1
  3. SyntaxError: can't assign to literal
  4. >>> 
It should be done similar to:
Expand|Select|Wrap|Line Numbers
  1. for fizz in expression_list:
Note that expression_list is evaluated once and should yield an iterable object. In your case, fizz_count is a function object, not an iterable object, and the expression will fail.
Jun 2 '13 #2
But shouldn't fizz be in quotes because I'm looking for the word among a list of 'x'. What would expression list be in this case? I'm still a bit confused. And also, is my x input okay right now? I feel like I should make it a list variable but I'm not quite sure how.
Jun 2 '13 #3
bvdet
2,851 Expert Mod 2GB
Something like the following?
Expand|Select|Wrap|Line Numbers
  1. >>> def fizz_count(x):
  2. ...     count = 0
  3. ...     for word in x:
  4. ...         if word == 'fizz':
  5. ...             count += 1
  6. ...     return count
  7. ... 
  8. >>> x = ['bizz', 'fizz', 'tizz', 'fizz']
  9. >>> fizz_count(x)
  10. 2
  11. >>>
The same thing can be accomplished using list method count.
Expand|Select|Wrap|Line Numbers
  1. >>> x = ['bizz', 'fizz', 'tizz', 'fizz']
  2. >>> x.count('fizz')
  3. 2
  4. >>> 
From what data is x derived?
Jun 2 '13 #4
Thanks guys I redid it and this
# Write your function below!
Expand|Select|Wrap|Line Numbers
  1. def fizz_count(x):
  2.     count = 0
  3.     for word in x:
  4.         if word == 'fizz':
  5.             count +=1
  6.     return count
  7.  
worked
Jun 2 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Donald Canton | last post by:
Hi, I'm using Bjarne's book to learn C++ and am stuck on the Calc program in Section 6. Everything works fine except when I try to use istringstream to parse a token from the command line. I...
12
by: ColinWard | last post by:
Hi. I am trying to run the following code when the user clicks a button, but I am getting a syntax error in the SQL. I have a feeling it has to do with brackets. Can anyone help? here is the...
11
by: KavvY | last post by:
Hi Can anyone tell me why I get a SQL syntax error with the following code? string strInsert = "INSERT INTO dateEntry (entry, date) VALUES ('test3', '17/08/2004')"; OleDbCommand cmd = new...
1
by: annbb | last post by:
I have a piece of code which resets all the control on a form ..but if I also put an option group on the form and the relevant code in the routine to reset it I get an error message saying that...
4
kestrel
by: kestrel | last post by:
I have some html code that is supposed to be displayed by php echo. But for some reason i keep getting a syntax error, and i cant figure out what is going on. Heres what i have <?php...
7
by: Dustin MacDonald | last post by:
Hi everyone. This is my first time posting to this newsgroup, and although I maintain my netiquette I might've missed something specific to the newsgroup, so hopefully you can avoid flaming me...
4
by: cluce | last post by:
I am getting a syntax error but I cant seem to spot it. need help with this. Its when I click the save button that fires my SQL UPDATE query. thansk in advance 'module level declarations Dim...
3
by: sclarkstone | last post by:
Im getting this error; Parse error: syntax error, unexpected T_STRING, expecting ':' or ';' with this line; header ('postcodesearch.php?e=nw&pcode=',); I cant find whats wrong, can anyone...
17
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
0
by: Daniel Halvorse | last post by:
hi I am working on an application that manages the data in a access database. but i cant seem to get past this error. When i try to run it it says: "Syntax error in INSERT INTO statement" syntax...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.