I am using Python 3.5 idle.
I am getting an indentation error while using elif function.
This is the code that I've entered:
>>> num = 7
>>> if num == 5:
print("number is 5")
... elif num == 11:
File "<stdin>", line 2
elif num == 11:
^
IndentationError: expected an indented block
You need to write some code after your elif. For example:
>>> num = 7
>>> if num == 5:
... print("Number is 5")
... elif num == 11:
... print("Number is 11")
...
>>> if num == 5:
... print("Number is 5")
... elif num == 11:
... print("Number is 11")
... elif num == 7:
... print("Number is 7")
...
Number is 7
>>>
This happens when you press enter after elif num == 11:
>>> if num == 5:
... print("Number is 5")
... elif num == 11:
...
File "<stdin>", line 4
^
IndentationError: expected an indented block
>>>
If you don't want to do anything for some reason in that case use pass
>>> num = 11
>>> if num == 5:
... print("Number is 5")
... elif num == 11:
... pass
... elif num == 7:
... print("Number is 7")
...
>>>
Related
while True:
month_num = int(input("enter the month number: "))
if month_num == 1:
print("january")
elif month_num == 2:
print("February")
elif month_num == 3:
print("March")
elif month_num == 4:
print("April")
elif month_num == 5:
print("May")
elif month_num == 6:
print("June")
elif month_num == 7:
print ("July")
elif month_num == 8:
print ("August")
elif month_num == 9:
print ("September")
elif month_num == 10:
print ("October")
elif month_num == 11:
print ("November")
elif month_num == 12:
print ("December")
else:
print("Enter a valid number")
i want to end the code cleanly but dont know to go about doind it i just stared it but i want to do it so that if i do put in the vlaues from 1 to 12 it would break after but i dont know how to do it efficently
import calendar
while True:
try:
month_num = int(input("enter the month number: "))
if 0 < month_num < 13:
print(calendar.month_name[month_num])
break
except ValueError:
pass
def getPreviousNumbers(num):
a = 0
if num <= a:
print("Negative Numbers & Zero Not Allowed")
else:
while num >= a:
num -= 1
if num == a:
continue
if num % 2 == 0:
print(num)
else :
print("Loop Is Done")
getPreviousNumbers(17)
I'm creating a program which I have to put a number and when I run the program, the solution should be the day for that number. But I don't know how to do. The program I did was this:
num = 4
if(num = 1)
print('Monday')
elif(num = 2)
print('Tuesday')
elif(num = 3)
print('Wednesday')
elif(num = 4)
print('Thursday')
elif(num = 5)
print('Friday')
elif(num = 6)
print('Sunday')
elif(num = 7)
print('Saturday')
elif(num < 1)
print('Put a number between 1 and 7')
else(num > 7)
print('Put a number between 1 and 7')
In python, for statements like if, for, etc. You have to add : at the end of it.
And for comparing (for equal) you have to use == and not =
num = 4
if(num == 1):
print('Monday')
elif num == 2:
print('Tuesday')
.....
You can compare without parenthesis and it will work too.
num = 0
while num <= 7:
num += 1
if num == 1:
print('Monday')
elif num == 2:
print('Tuesday')
elif num == 3:
print('Wednesday')
elif num == 4:
print('Thursday')
elif num == 5:
print('Friday')
elif num == 6:
print('Saturday')
elif num == 7:
print('Sunday')
I'm trying to make a random generator of numbers and letters go into one line after being in a string, for example from:
a
b
c
Like so...:
To "abc"
I was wondering if there was any way of doing that. The code I use is:
import random
import time
amount = int(input("How many digits do you want the password to be?\n"))
time.sleep(0.5)
print("============================================================\n")
for count in range(amount):
time.sleep(0.1)
num = random.randint(1,62)
if num == 1:
a = print("1")
if num == 2:
print("2")
if num == 3:
print("3")
if num == 4:
print("4")
if num == 5:
print("5")
if num == 6:
print("6")
if num == 7:
print("7")
if num == 8:
print("8")
if num == 9:
print("9")
if num == 10:
print("a")
if num == 11:
print("b")
if num == 12:
print("c")
if num == 13:
print("d")
if num == 14:
print("e")
if num == 15:
print("f")
if num == 16:
print("g")
if num == 17:
print("h")
if num == 18:
print("i")
if num == 19:
print("j")
if num == 20:
print("k")
if num == 21:
print("l")
if num == 22:
print("m")
if num == 23:
print("n")
if num == 24:
print("o")
if num == 25:
print("p")
if num == 26:
print("q")
if num == 27:
print("r")
if num == 28:
print("s")
if num == 29:
print("t")
if num == 30:
print("u")
if num == 31:
print("v")
if num == 32:
print("w")
if num == 33:
print("x")
if num == 34:
print("y")
if num == 35:
print("z")
if num == 36:
print("A")
if num == 37:
print("B")
if num == 38:
print("C")
if num == 39:
print("D")
if num == 40:
print("E")
if num == 41:
print("F")
if num == 42:
print("G")
if num == 43:
print("H")
if num == 44:
print("I")
if num == 45:
print("J")
if num == 46:
print("K")
if num == 47:
print("L")
if num == 48:
print("M")
if num == 49:
print("N")
if num == 50:
print("O")
if num == 51:
print("P")
if num == 52:
print("Q")
if num == 53:
print("R")
if num == 54:
print("S")
if num == 55:
print("T")
if num == 56:
print("U")
if num == 57:
print("V")
if num == 58:
print("W")
if num == 59:
print("X")
if num == 60:
print("Y")
if num == 61:
print("Z")
This basically creates the string of how many characters you want but I can't get them all on one line.
You can use the string module.
Example Code:
import string
print(string.ascii_letters+string.digits)
You can then get a certain number of random letters and numbers using this code:
import random
import string
number_of_letters_and_digits = 5
print(''.join([random.choice(string.ascii_letters+string.digits) for i in range(number_of_letters_and_digits)]))
Completed Code:
import random
import string
import time
amount = int(input("How many digits do you want the password to be?\n"))
time.sleep(0.5)
print("============================================================\n")
print(''.join([random.choice(string.ascii_letters+string.digits) for i in range(amount)]))
If you need to use the code that you showed, you could append the letters to the string (replace print("letter") with listName.append("letter")). Then, after the for loop, you could run print(''.join(listName)).
def program():
print ("please make sure your answer is spelt correctly")
print ('Start program')
choice = input("please select either a card or coin?")
if choice == "COIN" or "coin" or "Coin":
print ("you will now be given heads or tails")
import random
higher_value = 2
lower_value = 1
final_value = random.randint (lower_value, higher_value)
if final_value == 1:
print ("Heads")
elif final_value == 2:
print ("Tails")
#CARD
else:
if choice == "Card" or "card" or "CARD":
print("you will now be given a number for; number,suit")
import random
higher_value = 13
lower_value = 1
final_value = random.randint (lower_value, higher_value)
if final_value == 1:
print ("ace")
if final_value == 2:
print ("2")
if final_value == 3:
print ("3")
if final_value == 4:
print ("4")
if final_value == 5:
print ("5")
if final_value == 6:
print ("6")
if final_value == 7:
print ("7")
if final_value == 8:
print ("8")
if final_value == 9:
print ("9")
if final_value == 10:
print ("10")
if final_value == 11:
print ("Jack")
if final_value == 12:
print ("Queen")
if final_value == 13:
print ("King")
import random
higher_value = 4
lower_value = 1
final_value = random.randint (lower_value, higher_value)
if final_value == 1:
print ("Hearts")
if final_value == 2:
print ("Clubs")
if final_value == 3:
print ("Spades")
if final_value == 4:
print ("Diamonds")
#REPEAT LOOP
flag = True
while flag:
program()
flag = input('Would you like to run the program again? [y/n]') == 'y'
print ("The program will now terminate.")
print ("Have a good day")
I am currently trying to separate the two parts of the program this is due to the fact that if I input card it will out put the results of coin toss and card pick this is quite frustrating. I can't see a reason why this may be happening so could someone please fix this for me, thanks :)
You have to correctly indent this part:
import random
higher_value = 2
lower_value = 1
final_value = random.randint (lower_value, higher_value)
if final_value == 1:
print ("Heads")
elif final_value == 2:
print ("Tails")
It must have the same indentation as print ("you will now be given heads or tails"), or else the interpreter will assume it exists outside the if statement
In the same manner you have to indent everything below print("you will now be given a number for; number,suit") correctly
Remember that python is different than other languages in the way that you do not use curly braces or begin..end statements to show a block of code. Rather the interpreter understands blocks of code through indentation, so you have to be careful with it.
EDIT: To further explain it, you must understand the difference of the following pieces of code:
i=1
j=2
if i==1:
print i
if j==10:
print j
else: #<--- this corresponds to if i==1:
print j
This will print 1
However, by just changing the indentation:
i=1
j=2
if i==1:
print i
if j==10:
print j
else: #<--- now this corresponds to if j==10:
print j
This will print 1 2
EDIT 2:
if choice == "COIN" or "coin" or "Coin":
This must be changed to
if choice == "COIN" or choice == "coin" or choice == "Coin":
Or else it will always evaluate to true
In the same manner change : if choice == "Card" or "card" or "CARD":
EDIT 3:
This works for me
import random
def main():
print ("please make sure your answer is spelt correctly")
print ('Start program')
choice = input("please select either a card or coin?")
if choice == "COIN" or choice == "coin" or choice == "Coin":
print ("you will now be given heads or tails")
higher_value = 2
lower_value = 1
final_value = random.randint (lower_value, higher_value)
if final_value == 1:
print ("Heads")
elif final_value == 2:
print ("Tails")
#CARD
else:
if choice == "Card" or choice == "card" or choice == "CARD":
print("you will now be given a number for; number,suit")
higher_value = 13
lower_value = 1
final_value = random.randint (lower_value, higher_value)
if final_value == 1:
print ("ace")
if final_value == 2:
print ("2")
if final_value == 3:
print ("3")
if final_value == 4:
print ("4")
if final_value == 5:
print ("5")
if final_value == 6:
print ("6")
if final_value == 7:
print ("7")
if final_value == 8:
print ("8")
if final_value == 9:
print ("9")
if final_value == 10:
print ("10")
if final_value == 11:
print ("Jack")
if final_value == 12:
print ("Queen")
if final_value == 13:
print ("King")
higher_value = 4
lower_value = 1
final_value = random.randint (lower_value, higher_value)
if final_value == 1:
print ("Hearts")
if final_value == 2:
print ("Clubs")
if final_value == 3:
print ("Spades")
if final_value == 4:
print ("Diamonds")
if __name__ == "__main__":
main()