Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
When I try to run my code it says I have invalid syntax with my else statement, but I can't figure out what's wrong.
import random
import time
username = input("Hello. Please enter your name, then press 'enter'.
After you type something, you will need to /n"
"click the 'enter' key to send it")
print ("Hello " + username)
time.sleep(3)
game_tutorial_input = input("Do you wish to see the tutorial? (y/n)")
if game_tutorial_input == "y":
print ("Great! Press enter after each instruction to move /n"
"onto the next one.")
else
print("Are you sure? (y/n")
indent matters in python , make sure the else is on the same indent column as the if. and as stated you need a : at the end of else.. ie, else:
Try 'else:'
I believe it's missing a colon.
indent out the else
add a colon to end of the else
Explanation:
else, if, elif, with etc.. are statements which need colons after the statement
add colon after else and indentation of else is wrong
if game_tutorial_input == "y":
print ("Great! Press enter after each instruction to move /n"
"onto the next one.")
else:
print("Are you sure? (y/n")
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm a beginner in python and coding in general.
I was messing around with if statements and whenever I ran this code, it would never print what I asked it to, it would only print what was in the else statement.
I'm putting my code down below and if someone could help me understand the issue that'd be great, thanks!
canyou = input("Can you make a good meal on your own? ")
canyou1 = input("Can you wash your clothes well? ")
canyou2 = input("Can you clean the house properly? ")
if canyou.upper == "no" and canyou1.upper == "no" and canyou2.upper == "no":
print("You need your mama")
else:
print("You don't need your mama")
You're comparing the uppercase string to a lowercase example. To fix this you can do the following
if canyou.lower() == "no" and canyou1.lower() == "no" and canyou2.lower() == "no":
print("You need your mama")
else:
print("You don't need your mama")
You also need to call the method by adding parenthesis after upper/lower, as shown in my example.
You need to call the method of each string like this:
canyou = input("Can you make a good meal on your own? ")
canyou1 = input("Can you wash your clothes well? ")
canyou2 = input("Can you clean the house properly? ")
if canyou.lower() == "no" and canyou1.lower() == "no" and canyou2.lower() == "no":
print("You need your mama")
else:
print("You don't need your mama")
Also you need to call .lower()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So, I am trying to learn Python and am using Python 3.6.0a3 on windows 10. As part of my practice I was trying to use the "while" loop and the editor says there is an error with my code and the cursor goes to 'While True"
My code is:
#Program to practice While and Continue
While True:
Print('Who are you?')
name=input()
If name!='Joe':
continue
print('Hello Joe! What is the password? (It is a fish)')
password-input()
If password=='swordfish'
break
print('Access granted')
I can't execute it!
Here's a working example:
#Program to practice While and Continue
while True:
print('Who are you?')
name = input()
if name != 'Joe':
continue
print('Hello Joe! What is the password? (It is a fish)')
password = input()
if password == 'swordfish':
break
print('Access granted')
You had a bunch of syntax errors:
Python is case-sensitive, so if, while and print should all be in lower-case.
Conditional statements need to end with a colon :
Statements inside a conditional block need to be indented.
Further reading:
https://www.python.org/dev/peps/pep-0008/
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm new to Python and while trying to write a scrip that will keep asking questions about the user until the scrip gets FALSE,
I decided to check the scrip,of course it gave me an syntax error that told me the mistake was on the fifth lane, `a.
Now on that lane I tried to change the old value of a to a new value.
sadly, I can't understand the mistake that I made, can some one please check it and explain me what went wrong ?
#!/usr/bin/python
print "Hello, I'm wilfred and I'm an Artificial Intelligence\n"
a=str(raw_input("Do you want to be my friend? \n"))
if a=="yes":
a=str(raw_input("Yey ! my first friend,what is your name?\n"))
if a==str :
print "Nice name man!"
elif a==int :
print "bye!"
elif a=="no":
print "Well, nice to meet you anway, good bye now \n"
Your line
a=str(raw_input("Yey ! my first friend,what is your name?\n")
Indent this line so it is inside the 'if' statement
Add a ')' at the end of this line
You just need to indent the line. Your code should work fine. Keep learning python. It's awesome!!!!
#!/usr/bin/python
print "Hello, I'm wilfred and I'm an Artificial Intelligence\n"
a=str(raw_input("Do you want to be my friend? \n"))
if a=="yes":
a=str(raw_input("Yey ! my first friend,what is your name?\n"))
if a==str :
print "Nice name man!"
elif a==int :
print "bye!"
elif a=="no":
print "Well, nice to meet you anway, good bye now \n"
To further help with the test cases, I changed your string and int tests for you. "==" test is for value btw.
#!/usr/bin/python
print "Hello, I'm wilfred and I'm an Artificial Intelligence\n"
a=str(raw_input("Do you want to be my friend? \n"))
if a=="yes":
a=str(raw_input("Yey ! my first friend,what is your name?\n"))
if a.isalpha() :
print "Nice name man!"
elif a.isdigit() :
print "bye!"
elif a=="no":
print "Well, nice to meet you anway, good bye now \n"
A general structure for this kind of repeating loop is
while True:
a=str(raw_input(...))
if a=="whatever": break
# other responses to a
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
def cave():
global key
global response
print(''' You find yourself standing infront of a cave.
You venture into the cave to find a large door blocking
your path.
(insert key, turn around''')
response = input("Enter a command: ")
while response != 'insert key' or response != 'turn around':
if response =='insert key' or response == 'turn around':
break
print('Choose one of the options: ")
response = input()
if response == 'insert key':
if key == 1:
win()
else:
print('''You don't have a key. Get One!!''')
elif response == 'turn around' :
home()
That's almost always caused by mixing tabs and spaces. Check your file contents with an editor that can show you this, such as by using :set list in vi.
But you may also want to look at this line:
print('Choose one of the options: ")
You're starting your string with one quote type and ending it with another, which is valid is neither Python 2 nor 3.
Once I'd fixed that and ensured that indentation was correct, it worked fine for me. I had to add a mainline that called cave() but it ran without error albeit not doing much useful since I don't have any of the rest of your code.
If you are using Notepade+ or VIM then check where is the tabs and space.
Also make sure that string you started must be end with same character. If you start with '(Single quote) then end with same.
print('Choose one of the options: ")
Ablove line start with ' but end with ". That will give you syntext error.
The indent block may cause by when you paste the code, before the code is 4 blanks (not in ascii), the python cannot read it , you can delete it and add a 'tab'.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
the python idle is throwing a error at the print() function im not sure why heres the code..
password = "cow"
name = input()
input("MR PENGUIN: hello there i am Mr Penguin what is your name? ")
input("well, hello there"+name+"Tell me your password")
input("You: my password is, ")
input("MR PENGUIN: im little defh could you repeat that? ")
input("YOU: my password is, "
print("PC POLICE: STOP! dont ever trust penguins with your data becuase he just told every one that your password is "+ password)
input("Press Enter To Exit")
You are missing a parenthesis at the end of the input on the prior line.
Change:
input("YOU: my password is, "
to:
input("YOU: my password is, ")
For the record, your print was fine. Note that when you get a cryptic error, it is often something on the previous line.
This is because your input statement in the previous line is missing a closing paranthesis.
It should be:
input("YOU: my password is, ")
instead of
input("YOU: my password is, "