Whenever I try to use elif and else after if, it says invalid syntax. For example:
if number == "1"
print("123213")
elif number == "2"
print("23423")
else number == "3"
print("324234")
There are three mistakes
: is missing
Indent is missing
else cannot have a conditional
The code can be re-written as
if number == "1":
print("123213")
elif number == "2":
print("23423")
elif number == "3":
print("324234")
Complete working code
number = input("Enter a number - ")
if number == "1":
print("123213")
elif number == "2":
print("23423")
elif number == "3":
print("324234")
Output
Enter a number - 1
123213
Related
This is the code that does not seem to work:
choice = input()
if input() == "1":
circle()
elif input() == "2":
square()
elif input() == "3":
triangle()
while input() != ("1" or "2" or "3"):
print("You have not chosen a correct number. Please try again.")
choice = input()
if input() == "1":
circle()
elif input() == "2":
square()
elif input() == "3":
triangle()
Basically the part that checks the correct number has been input does not seem to work and I don't know why, its only a logic error and i think its something to do with this part:
while input() != ("1" or "2" or "3"):
print("You have not chosen a correct number. Please try again.")
while input() != ("1" or "2" or "3"): is not correct.
("1" or "2" or "3") is a logical statement and resolves to the first non-empty or non-zero item:
>>> "1" or "2" or "3"
'1'
So the statement resolves to:
while input() != '1':
To correct it, use not in and a tuple:
while input() not in ("1", "2", "3"):
Alternatively, use the following pattern when asking for input and have a dispatch table of functions to eliminate the multiple if statements:
def circle():
print('circle')
def square():
print('square')
def triangle():
print('triangle')
funcs = {'1':circle,
'2':square,
'3':triangle}
while True:
choice = input()
if choice in ('1','2','3'):
break
print("You have not chosen a correct number. Please try again.")
funcs[choice]()
It looks like you're storing the input as choice but then calling input() again instead of using the value you already read.
Additionally, you can simplify this code significantly.
You should instead do the following:
choice = input()
while choice not in ("1", "2" ,"3"):
print("You have not chosen a correct number. Please try again.")
choice = input()
if choice == "1":
circle()
elif choice == "2":
square()
elif choice == "3":
triangle()
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
So i am making a quick dice roller (for D&D) and when I try to roll dice it only generates numbers from 1 to 4!
import random
def d4():
x = (random.randint(1,4))
print(x)
def d6():
x = (random.randint(1,6))
print(x)
def d8():
x = (random.randint(1,8))
print(x)
def d10():
x = (random.randint(1,10))
print(x)
def d12():
x = (random.randint(1,12))
print(x)
def d20():
x = (random.randint(1,20))
print(x)
def d100():
x = (random.randint(1,100))
print(x)
choice = input("What dice will you roll: ")
if choice == "d4" or "4":
d4()
elif choice == "d6" or "6":
d6()
elif choice == "d8" or "8":
d8()
elif choice == "d10" or "10":
d10()
elif choice == "d12" or "12":
d12()
elif choice == "d20" or "20":
d20()
elif choice == "d100" or "100":
d100()
else:
print(random.randint(1,20))
For example when I put my input as d100 I can only get 1,2,3, or 4!
what on earth is going on?!
Your if statements are not correct, they should be like this:
if choice == "d4" or choice == "4":
d4()
The way you did it makes "4" evaluate to True, so the if always enters to the first statement, which is for D4.
This is the code i have made and whatever the score is the output is always "Grade: ", I want it to output a letter equevalent but it will not. I have tried using commas but nothing seems to work. I have tied looking around but cannot find a answer to my problem.
score = input
grade = ""
if score == "5":
grade == "A"
if score == "4":
grade == "B"
if score == "3":
grade == "C"
if score == "2":
grade == "D"
if score == "1":
grade = "E"
if score == "0":
grade == "F"
print("Grade:" + grade)
You need to :
1- use one operator = to assign.
2- Assign a value to score via input()
score = input("enter the score (0-5):")
grade = ""
if score == "5":
grade = "A"
if score == "4":
grade = "B"
if score == "3":
grade = "C"
if score == "2":
grade = "D"
if score == "1":
grade = "E"
if score == "0":
grade = "F"
print("Grade:" + grade)
Assignment uses a single equal sign, comparison uses two.
(When you're assigning the variable, only use one equal sign)
I am attempting to make a simple Python code that replaces numbers with roman numerals. In order to do this, I need to get the position of each number to replace it with the roman numeral equivalent. However, my code doesn't seem to work.
number = range(1,21)
number = list(number)
number = str(number)
for i in number:
for x in i:
if i.index(x) == 0:
if x == "1":
x.replace(x, "X")
elif x == "2":
x.replace(x, "XX")
else:
if x == 1:
x.replace(x, "I")
elif x == 2:
x.replace(x, "II")
elif x == 3:
x.replace(x, "III")
elif x == 4:
x.replace(x, "IV")
elif x == "5":
x.replace(x, "V")
elif x == "6":
x.replace(x, "VI")
elif x == "7":
x.replace(x, "VII")
elif x == "8":
x.replace(x, "VIII")
elif x == "9":
x.replace(x, "IX")
else:
x.replace(x, "")
print number
I suspect that it has to do with the way that my if statements work, but I'm not sure. Any advice would be appreciated.
A long sequence of if and elif clauses is usually a sign that one should be using one or more dicts.
numstrings = [str(i) for i in range(1, 100)]
d0 = {'0':'', '1':'I', '2':'II', '3':'III', '4':'IV',
'5':'V', '6':'VI', '7':'VII', '8':'VIII', '9':'IX'}
d10 = {'0':'', '1':'X', '2':'XX', '3':'XXX', '4':'XL',
'5':'L', '6':'LX', '7':'LXXX', '8':'LXXX', '9':'XC'}
for s in numstrings:
if len(s) == 1:
r = d0[s]
elif len(s) == 2:
r = d10[s[0]] + d0[s[1]]
else:
r = '??'
print(r)
My problem is that my Random function doesn't get called from the Menu function. I've tried everything, but it still doesn't work. It's weird, because i think that i have structured the functions correctly, and everything should work fine, but it doesn't call Random.
def Menu():
name = raw_input("What's your name?\n:")
print "Hello, %s!\nWeclome to the Guessing Game!" % name
print "Select an option:"
print "1- Guess a number between 1-100."
print "2- Guess a number between 1-1000."
print "3- Guess a number between 1-10,000."
print "4- Guess a number between 1-100,000."
print "5- Exit."
try:
selection = raw_input("Enter your selection:")
if selection == 1:
Random(100)
elif selection == 2:
Random(1000)
elif selection == 3:
Random(10000)
elif selection == 4:
Random(100000)
elif selection == 5:
exit()
except:
os.system("clear")
print "Sorry, that wasn't an option. Enter your selection again."
Menu()
raw_input() retuns a string so you have to cast the input to int or compare the input with strings. Either this way:
selection = raw_input("Enter your selection:")
if selection == "1":
Random(100)
elif selection == "2":
Random(1000)
elif selection == "3":
Random(10000)
elif selection == "4":
Random(100000)
elif selection == "5":
exit()
or this way:
selection = raw_input("Enter your selection:")
if int(selection) == 1:
Random(100)
elif int(selection) == 2:
Random(1000)
elif int(selection) == 3:
Random(10000)
elif int(selection) == 4:
Random(100000)
elif int(selection) == 5:
exit()
Furthermore you can avoid try by kicking out elif int(selection) == 5: and using else:" instead. So the game will be ended with any other input than 1,2,3 or 4. There will be no possibility to "enter your selection again" after calling except in your code anyway because the script stops.
The function Random is not very optimal. See this:
def Random(select):
range = "1-" + str(select)
Correct_Guess = random.randint(1,select+1)
Difficulty()
It is the same but shorte and more readable ;)