elif statement SyntaxError: invalid syntax [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 4 years ago.
Improve this question
Could somebody kindly let me know why is there a SyntaxError? I use macOS Mojave and have installed IDLE version 3.7.1.
if name == 'Alice':
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')
elif age > 2000:
print('Unlike you, Alice is not undead, immortal vampire.')
elif age > 100:
print('You are not Alice, grannie.')

Indentation is wrong:
name = input('whats your name? ')
age = int(input('whats your age? '))
if name == 'Alice':
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')
elif age > 2000:
print('Unlike you, Alice is not undead, immortal vampire.')
elif age > 100:
print('You are not Alice, grannie.')

'elif' should have the same indent as the 'if'
remove white space before your elif
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else

Related

My very basic python program will not work and I don't know why maybe I am blind and tired but I can't tell [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 7 months ago.
Improve this question
why do i get this error for this:
prisoners = input('How many player(s): ')
print = ('Welcome to Prison break!')
if int(prisoners) == 1:
print('Welcome solo player, bravest of the brave')
elif 2 <= int(prisoners) <5 :
print('Welcome prisoners, hold on to each other tight')
elif 1 > int(prisoners):
print('Whoops, this game is 1-4 players. ')
elif 4 < int(prisoners):
print('Whoops, this game is 1-4 players. ')
err:raceback (most recent call last):
File "/Users/aiyoobaman/Documents/vscode/stuff.py", line 7, in <module>
print('Welcome solo player, bravest of the brave')
TypeError: 'str' object is not callable
Please help.
You need to change the second line
prisoners = input('How many player(s): ')
print('Welcome to Prison break!')
if int(prisoners) == 1:
print('Welcome solo player, bravest of the brave')
elif 2 <= int(prisoners) <5 :
print('Welcome prisoners, hold on to each other tight')
elif 1 > int(prisoners):
print('Whoops, this game is 1-4 players. ')
elif 4 < int(prisoners):
print('Whoops, this game is 1-4 players. ')

If/Elif Python Statement [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 2 years ago.
Improve this question
New to this. My autochecker says that line 3 is incorrect.
score = input("Enter Score: ")
s = float(score)
if s >= 0.9
("A")
elif s >= 0.8
print ("B")
elif s >=0.7
print ("C")
elif s >=0.6
print ("D")
elif s < 0.5
print ("F")
else
print ("Enter number")
You need a colon after each if/elif/else line.
You need to add print before "A".

Python says that elif statement is a Syntax Error [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 3 years ago.
Improve this question
I am new to python, and I have just started to learn from a book that I have recently bought.
I am at the if, elif and else chapter.
I have copied the code from the book, and I ran it.
Suddenly, it says about "elif", that is a Syntax Error.
Please help
num = int(input('Please Enter A Number:'))
if num > 5:
print('Number Exceeds 5')
elif num < 5:
print('Number is Less than 5')
else:
print('Number is 5')
In Python, indentation is key. You need to make sure your indentation is proper. Please change your code as follows, it should work.
In your code elif is not a part of your if block, therefore, it should be outside it.
num = int(input('Please Enter A Number:'))
if num > 5:
print('Number Exceeds 5')
elif num < 5:
print('Number is Less than 5')
else:
print('Number is 5')
In Python indenting matters because it is used to control code execution flow.
Your if, elif, and else statements in your code should all be at the same level of indent:
num = int(input('Please Enter A Number:'))
if num > 5:
print('Number Exceeds 5')
elif num < 5:
print('Number is Less than 5')
else:
print('Number is 5')

NameError: name 'grade' is not defined [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 4 years ago.
Improve this question
I am trying to write a program that will number grade output when the user enters a letter grade. I am getting this error: NameError: name 'grade' is not defined. Can someone tell me what this error means and help me figure out how to fix it. I am new to programming so I am realy lost.
letterGrade = (input("Please enter a letter grade: "))
if grade >= A:
grade == "4.0"
elif grade < A:
grade < "4.0"
print(grade)
It looks like your if statements are checking for the variable grade, but your input is being assigned to letterGrade. You can fix this either by changing your first line to
grade = input("Please enter a letter grade: ")
or change every instance of grade to letterGrade.
Additionally, grade == "4.0" should probably be grade = "4.0"; x == y checks if x is equal to y, whereas x = y sets x equal to y.

Using random in an if statement [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 am trying to make a random number guessing game but I cant get the if statement to check if the users input is = to the random number
import random
realNumber = random.randint(1, 50)
print(realNumber)
myNumber = print(input("Guess the number from 1 to 50: "))
if int(myNumber) == realNumber:
print("You win")
else:
print("Nope guess again")
The unintended behavior of your program is due to this line:
myNumber = print(input("Guess the number from 1 to 50: "))
Here, you are trying to assign myNumber to the return value of the print statement (Which is None) and not the value obtained from the input() statement. To fix this, simply remove the print() around the input.
myNumber = input("Guess the number from 1 to 50: ")
Hope this helped!
You don't need the print statement around input.
import random
realNumber = random.randint(1, 50)
print(realNumber)
myNumber = input("Guess the number from 1 to 50: ")
if int(myNumber) == realNumber:
print("You win")
else:
print("Nope guess again")
Note that this code will not work if the user enters something besides an integer, because the int() call will not cast correctly

Categories