Python- Else error [closed] - python

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
I am very bad at python [Learning to code] and I made a guess-a-number script, but when I ran it I got this:
File "guess-a-num.py", line 15
else:
^
SyntaxError: invalid syntax
Here is the part I probably messed up on:
if ask == number:
print("You got it!")
print("Thanks for playing!")
raw_input("")
quit
else:
print("Nope! 3 tries left!")
ask == raw_input("?")
if ask == number:
print("You got it in 2 tries!")
raw_input()
quit

if ask == number:
print("You got it!")
print("Thanks for playing!")
raw_input("")
quit
else:
print("Nope! 3 tries left!")
ask == raw_input("?")
if ask == number:
print("You got it in 2 tries!")
raw_input()
quit

Related

Working on a python project and there are syntax errors never faced before [closed]

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
def main_menu():
print("WELCOME TO LIBRARY")
print("What would you like to do?")
option=print(input("1. New user \n 2. Existing user ")
if option == 1:
new_user()
elif option == 2:
old_user()
else:
print("please choose a correct value")
main_menu()
def new_user():
print("new user")
def old_user():
print("Old user")
main_menu()
Here is a picture of the error, it says syntax error missing ")" for function call and missing "else" for the first one at "if":
the line
option=print(input("1. New user \n 2. Existing user ")
is missing a closing )
it should be
option=print(input("1. New user \n 2. Existing user "))

Python is stopping before running a loop [closed]

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 5 years ago.
Improve this question
I'm having trouble with this code. It will run normally until it gets to the loop (def Game) and just stops there. I did have it as an if statement but that still didn't work. Please note that this may be a bit messy.
import random
import time
GameInProgress = ("Yes")
TutorialDone = ("No")
ReplayGame = ("Yes")
#Test purposes
PlayerName = ("Lewis")
print ("Welcome to 'Guess The Word!")
def Game():
GameInProgress = ("Yes")
TutorialDone = ("No")
ReplayGame = ("Yes")
#Test purposes
PlayerName = ("Lewis")
print ("Welcome to 'Guess The Word!")
WordSelected=("No")
LettersGuessed=0
print (TutorialDone)
EnterName = input("Would you like to enter your name?").title()
def Game(): is not a loop, it is a function it does not execute until you call it.
you can call a python function in this way
Game()
if you want to call the same function again and again simply you can call the function inside a for or while loop:
while(condition):
Game()
if your are very beginner follow some tutorials
https://www.tutorialspoint.com/python/python_functions.htm

python if variable == string wont work when you enter correct value [closed]

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 5 years ago.
Improve this question
Hi i am am trying to make a program where if you enter hi it will say you rule but if you enter anything else it will say sucker
my current code is
import sys
print("enter your password")
pword = (sys.stdin.readline())
if pword == "hi"
print("i rule")
else:
print("sucker")
please help!
just add strip to remove unwanted space/newline characters
import sys
print("enter your password")
pword = (sys.stdin.readline())
if pword.strip() == "hi":
print("i rule")
else:
print("sucker")
Use this instead:
pword = input()
Or if it's Python 2.x, use raw_input() instead.
But fix your indentation first, and add a colon after if pword == "hi".
no need to import sys(except you want to)
pword = input("enter your password")
if pword == "hi":
print("i rule")
else:
print("sucker")
If you want to the correct code is:(you forgotten an : in the if statment)
import sys
print("enter your password")
pword = (sys.stdin.readline())
if pword.strip() == "hi":
print("i rule")
else:
print("sucker")

Python - How do I get this to read the code? [closed]

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
Here is my code
import time
print("------------------------------------------")
print(" Welcome to Senpai Quest")
print('------------------------------------------')
time.sleep(3)
print("")
print('You have English next, there is a test. If you hit yes .you go to
class and you get +1 charisma and Knowledge.')
print("")
print('If you hit no, you will skip class and get +1 Courage and +1 risk.')
ans1=str(input('Do you Take the english test? [Y/N]'))
if ans1== ['y']:
print("It works")
else:
print("Woo Hoo!")
When it asks the question and for the 'y' it just goes straight through to "woo hoo!". What i would like it to do is to print "It works" but if you type n it just goes to woo hoo. Please help
This should work:
ans1 = input('Do you Take the english test? [Y/N]').strip()
if ans1.lower() == 'y':
print("It works")
else:
print("Woo Hoo!")
I suggest using the distutil's strtobool in order to cover all cases
from distutils.util import strtobool
ans1=input('Do you Take the english test? [Y/N] ').strip()
if strtobool(ans1):
print("It works")
else:
print("Woo Hoo!")

Python programming error, elif statment syntax error Simple code [closed]

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
name=input("Hello person, Whats your name?")
print("Hello", name)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
print("Ok", name,", listen up")
print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
choice2=input("What do you want to call the house?")
print("The old,",choice2,"was once owned by an old lady. ")
elif choice==("maybe"):
print("You found an easter egg, congrats. PS this does nothing")
Whats wrong with this code?? It says in the idle shell syntax error. The last elif statement isn't working.
This is a petty indentation issue, your print statements for the if blocks are not indented right and so the elif seems to be out of place. Note that python keeps track of logical blocks by the indentation.
name=input("Hello person, Whats your name?")
print("Hello", name)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
print("Ok", name,", listen up")
print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
choice2=input("What do you want to call the house?")
print("The old,",choice2,"was once owned by an old lady. ")
elif choice==("maybe"):
print("You found an easter egg, congrats. PS this does nothing")
As already pointed out, if choice==("yes" or "yes " or "Yes" or "Yes ") is wrong, use if choice.lower().strip() == "yes" instead, or if choice in ("yes", "yes ", "Yes", "Yes ").
If in case this is python 2, input will throw an error, use raw_input instead.
Also print with multiple statements will throw errors as well if used like a function, so change them from print(statement_x, statement_y, statement_z) to print statement_x, statement_y, statement_z

Categories

Resources