As part of a piece of python work I have to ask for a first name and surname, then repeat it three times if it is correct. When I try it Python puts a syntax error on the colon. What do I need to do to correct it?
Basically whats going on
I've already tried removing the colon, bringing the if line down to one equals, and looking for bracket errors, but I cant find anything.
The colon should be at the end,
if input == yes:
However I cannot see how this can work? Is that the full code? If so, what is the yes variable?
If it was intended to be a string, the line should be,
if input == "yes":
The "" tells Python that it is a string (a word).
A link to Python syntax: https://en.wikipedia.org/wiki/Python_syntax_and_semantics
Related
I'm new to python, I study it by myself, and I just want to make sure I understand something about find() function...
If I want to find a specific string, it will find the first index of the first letter/number of the string or the the first time this strings?
for exaple, there's the next given string:
"I$want$moeny$and$food"
If I type find('$')
it will give me the first time '$' showed up in this string, which is 1.
However, if I want to find "$moeny", I will get the the number 6, means python serched where the full "$moeny" string is showed in the first time?
and If I type find('$mommy') it will give me an Error, and not index 1.
Am I right? Did I understand it clearly?
Thanks a lot :)
The best answer is to try it yourself and see what happens.
A good practice is to enter the CMD or shell and type "python" and try various commands.
enter image description here
so I keep getting these whitespaces in my output. I have googled but nothing relevant comes back. i have attached my code and my output. can someone tell me what i am doing wrong. I dont have any space in my code, so why is it putting it in my ouput? this is a common problem form me in zybooks and i would really love to know what i am doing wrong so i can get this done and move on to the next assignment without spending 2 hours on a simple exercise.
If you're asking why there are spaces between your outputs of favoriteColor, etc., it's because Python's print function automatically adds spaces between each of its arguments. If you don't want this happening, you can change your code in two ways:
Either add a sep argument to your print, telling Python to add an empty separator between your arguments:
print('\nFirst password:', favoriteColor, chr(95), petName, sep='')
Or, you can append your arguments together yourself, as strings:
print('\nFirst password:', favoriteColor + chr(95) + petName)
Also, I should mention that if you need an underscore, you don't need to call chr(95) -- you can just write the string '_'.
I have made a program for a holiday csv file but it keeps coming up as an invalid syntax.
def summer_template:
summer_template == 1:
open("holiday.csv","w")
summer_list["shorts","sandels","shirt(s)","suncream"]
summer_extra=input("what do you want to do on holiday")
holiday_file=csv.writer(summer_list,summer_extra)
csvfile.close()
the error is located where the "summer_template" is
It's python, you need to have indentation done correctly. Try to copy the whitespace from the "open" line and replace the whitespace on the offending line.
And it looks like there's an if statement missing. If that's the case everything under if needs to be further indented. Remember you can't mix tabs and spaces either.
Are you missing an if statement? Using the == operator usually is done within an if statement.
https://docs.python.org/3.4/tutorial/controlflow.html
function definitions should have parentheses: def summer_template():.
Perhaps you mean if summer_template == 1:. In that case, everything you want to be in that if block needs to be indented.
What is wrong with my code?
It says there is an invalid syntax and highlighting the colon?
BTW, I'm doing computing GCSE and this is part of the coursework prep.
I want it to take a letter to repeat and then repeat it an inputted number of times.
letter=input("Please enter a letter to be repeated: ")
number=int(input("Please enter the number of times you want it repeated: ")
for a in range(0,number):
print(+letter)
Remember that when you're debugging, compilers and interpreters will report where an error was first detected, not necessarily where the error actually is. You're missing a closing parentheses on this line:
number=int(input("Please enter the number of times you want it repeated: ")
Add another ) to the end of that line. The interpreter is seeing the opening parentheses for the int function call, then is happily looking through the rest of the file to find its match. When it reaches the end of the file without the parentheses being balanced, it gives up and throws the exception.
As Josh points out, +letteris also invalid syntax. I'm not sure what you were trying to achieve with it so I can't recommend a specific fix, but it needs to go.
You are missing your closing parenthesis for the int() function call. Also you need to remove the + from print(+letter)
got a bit of a noob question.
I'm trying to get Metagoofil working because it keeps saying "error downloading webpage" etc etc.
A google search found that I can change a bit of the code in one of the config files and it will work properly again.
I'm having a problem though: this seems to be the code I want to use.
self.url = url.replace("/url?q=", "", 1).split("&")[0]
BUT, it doesn't seem to like me (based on syntax highlighting) have those two quotation marks together with nothing in between. When they are like above, it starts highlighting .split(" up to here thinking that this is the string.
My question is, how can I make the double quotations together without anything in the middle and have it register as its own string, so it doesn't highlight the .split("
The literal "" is a valid string with a length of zero. I guess your syntax highlighter is not working properly.