Simple elif statement wont work because of 'expression expected'? - python

I am very new to coding and just getting the jist of basic code.
I have done an if statement before but haven't come across this issue, could anyone help?
My code:
print("Hello User")
myName = input("What is your name?")
print("Hello" + myName)
myAge = input("What is your age?")
if input < 17:
print("Not quite an adult!")
elif:
print("So you're an adult!")
https://gyazo.com/15eef7751886747f4ce572641b9398fc

You need else:.
elif means else if and needs expression.
if expression:
pass
elif expression:
pass
else:
pass

Just change the 'elif' to 'else'. And there you go!

print("Hello User")
myName = input("What is your name?")
print("Hello" + myName)
myAge = int(input("What is your age?"))
if myAge < 17:
print("Not quite an adult!")
elif myAge >17:
print("So you're an adult!")
TypeError: '<' not supported between instances of 'str' and 'int'>>must castingtype into int for input>>>so when use elif he Suppose you complete the condition

Related

I'm trying to make a simple program in python, but im stuck

I'm trying to make a very simple program in python, but it says syntax invalid in an error window. I cant understand what's wrong with it:
print('What is your name?')
YourName = input()
if YourName == "Alice":
print('Hello Alice')
elif print('How old are you?')
age = input()
elif age < 4:
print('And you know how to write at young age!')
Try this:
print('What is your name?')
YourName = input()
if YourName == "Alice":
print('Hello Alice')
else:
print('How old are you?')
age = int(input())
if age < 4:
print('And you know how to write at young age!')
And a one-liner:
print("Hello Alice") if input("What is your name ")=="Alice" else print('And you know how to write at young age!') if int(input("How old are you? "))<4 else None
Problem with your Code
Welcome! The following line is wrong:
elif print('How old are you?')
There are two main reasons behind this:
print('How old are you?') is not a normal condition. Although this is technically fine since your compiler will evaluate it as type None and treat it as if it is False, you might want to replace that part with something that is conceptually meaningful.
You're missing a colon after the elif. For example:
elif (int(input()) < 4):
#your code
Solution
print('What is your name?')
YourName = input()
if YourName == "Alice":
print('Hello Alice')
else:
print('You are not Alice! How old are you?')
if int(input()) < 4:
print('You know how to write at young age!')

Using variables from separate functions in Python?

I am currently working on a coding project where I have set several functions. I am trying to figure out how to use variables from different function. I have tried using 'self' but it hasn't worked. Can anyone help me sort out my code?
class A(object):
def intro1(self):
print("Welcome to the XXCI forum!")
self.intro2()
def intro2(self):
print("Press 'L' to log in and 'S to sign up.")
ch1 = input()
if ch1 == 'l' or 'L':
self.log_in()
elif ch1 == 'S' or 's':
self.sign_up1()
else:
print("Your input was invalid")
self.intro2()
def sign_up1(self):
print("Please enter your first name:")
fn1 = input()
if len(fn1) >= 3:
self.sign_up2()
elif len(fn1) <3:
print("Please enter a name that is equal to or over three characters!")
self.sign_up1()
def sign_up2(self):
print("Please enter your last name.")
ln1 = input()
if ln1 == ln1:
with open(ln1.txt, "a") as ln1:
ln1.write("Age: " + ag1 + ".")
sign_up3()
def sign_up3():
print("Please enter your age.")
ag1 = input()
if ag1 > 90:
print("Please enter an age under 90 and equal to 16 or over.")
elif ag1 <16:
print("Please enter an age under 90 and equal to 16 or over.")
else:
user_g()
def user_g():
username = (fn1[3], str[ag1])
username.join''
print("Here is your username:")
print(username)
p_creator()
It's obviously not finished yet, but I would really appreciate guidance with how to correct my errors and use variables in different functions. Thank you very much!!
you can find everything in the Python documentation: https://docs.python.org/3/tutorial/classes.html?highlight=class%20attributes%20access#class-objects
So you need just create and then call class's attributes.
Also you could use the best python practice DRY (don't repeat yourself) and merge two 'if' statements in def sign_up3 like this: 'if ag1 > 90 and ag1 < 16' (another and shorter way is 'if 16 < ag1 < 90')
Hope it helped anyhow.
best regards and good luck.
P.S.: please, use the indentations.
You can use global variable. Assign the variable outside the function. Then inside the function called the variable with global in front of it. For example:
def f():
global s
print(s)
s = "Demo of global variable"
f()

Why is this simple code wrong?

print("Hello there")
name=input("What is your name?")
print("Welcome to the some game, " + name + "!")
print("I'm going to ask you some basic questions so that we could work together")
age=input("Your age")
if age >= 14 and age < 41:
print("K")
else:
print("Sorry bruh")
print("Thanks")
keeps showing me "Sorry bruh" at the end when entered 15. Why? What's wrong?
Cast your input to int:
age = int(input("Your age"))
You could add a try-except. Your condition should equally be evaluated on or not and
Either you use Python3 and you need to cast with int(input("Your age")) or you use Python2 and then you need to use raw_input to read the name: name=raw_input("What is your name?")
Ok , so you can try this code in python2.7 and it will work as you desired:
print("Hello there")
name=raw_input("What is your name?")
print("Welcome to the some game, " + name + "!")
print("I'm going to ask you some basic questions so that we could work together")
age=input("Your age")
if age >= 14 and age < 41:
print("K")
else:
print("Sorry bruh")
print("Thanks")

Fixing maths quiz in python

MY maths quiz was working but now. It says after 'if ops =='+':' that the colon is an invalid syntax. Please can you help me fix it. If I remove the colon below it the answer variable would come up as an invalid syntax error.
import random
question=0
userInput = int()
LastName = str()
answer = str()
FirstName = str()
Form = str()
def Quiz():
score=0
LastName = input ("Please enter your surname: ").title()
FirstName = input ("Please enter your first name: ").title()
Form = input ("Please enter your form: ").title()
for i in range(10):
print ("What is:")
num1= random.randint(1,12)
num2= random.randint(1,12)
ops = ['+', '-', '*']
operation = random.choice(ops)
Q = int(input(str(num1)+operation+str(num2)
if ops =='+':
answer==num1+num2
if Q == answer:
print ("correct")
score= score+1
else:
print('You Fail')
elif ops =='-':
answer==num1-num2
if Q == answer:
print ("correct")
score= score+1
else:
print("you fail")
else:
answer==num1*num2
if Q == answer:
print ("correct")
score= score+1
else:
print("you fail")
You are missing two parenthesis on this line:
Q = int(input(str(num1)+operation+str(num2)))
,This will lead into a syntax error in the next line.
ALSO:
Remove the double = in lines like this:
answer=num1+num2 #it was == before
Declare "score" before starting the game:
score = 0
Replace "ops" with "operation":
if operation == '+':
Use if operation=='+'. ops is a list, you want to match char (operation).

How do I get this function to read the input properly?

I am having trouble with my monster_room function. No matter what the input is, it will always choose the elif choice (elif choice > 5:...). Here is the function:
def monster_room():
print "This is the monster room! How many monsters do you see?"
choice = raw_input("> ")
if choice < 5:
dead("More than that idiot!")
elif choice > 5:
print "More than you thought right?!"
print "Do you go to the Alien room or Frankenstien room?"
choice_one = raw_input("> ")
if choice_one == "alien":
alien_room()
elif choice_one == "frankenstien":
frankenstien_room()
else:
print "Type alien or frankenstien, DUMMY!"
monster_room()
else:
print "Type a number!"
monster_room()
How do I get it to read the input? Also this is my first project and I know it's very basic and probably looks rough so other tips I am open to as well. If you need the full code (99 lines) just let me know! Thanks!
You need to parse your input data to a number type using int() because raw_input() returns a string.
choice = int(raw_input("> "))
As #Joran Beasley metniioned to be on the save side you shuld use a try except block:
try:
choice = int(raw_input("> "))
except ValueError:
print "Type a number!"
monster_room()
you'll get to the "type a number!" section even if you input 5 since you're not allowing a >= or <= scenario.
I do agree, though, you have to convert the input to an int or float.
Casting the input to int will solve it (it's a string when you use it). Do this:
choice = int(raw_input("> "))

Categories

Resources