Reputation: 13
Hi this is school work so please don't just give me the right answer but tell me what I have done wrong so I can go and correct it please. So my code works fine but there is one problem. When I type "Coin", the error message is displayed after it displays the coin flip. Here is my code:
def program():
error_message = "Answer not recognised!\nMake sure it is spelt right and starts with a capital letter."
Choice = input("Pick either 'Card' or 'Coin' ")
if Choice == "Coin":
import random
r = random.randint(1,2)
if r == 1:
print ("Heads!")
else:
print ("Tails!")
if Choice == "Card":
import random
card_number = random.randint(1,13)
if card_number == 1:
card_number = "Ace"
elif card_number == 11:
card_number = "Jack"
elif card_number == 12:
card_number = "Queen"
elif card_number == 13:
card_number = "King"
if Choice == "Card":
import random
card_suit = random.randint(1,4)
if card_suit == 1:
card_suit = "Hearts"
elif card_suit == 2:
card_suit = "Clubs"
elif card_suit == 3:
card_suit = "Diamonds"
else:
card_suit = "Spades"
if Choice == "Card":
print(card_number ,"of" , card_suit)
#else print error message
else:
print(error_message)
while flag:
program()
flag = input('Would you like to run the program again? [yes/no] ') == 'yes'
else:
print ("The program will now terminate.")
I have tried many different ways of trying to get around this but I cant figure it out. Remember this is school work so I would like help but not to be told the answer. Thanks
Upvotes: 0
Views: 49
Reputation: 13
Thank you everyone my code works now, I am now just trying to shorten it. Thank you Here is the completed code:
import random
flag = True
def program():
error_message = "Answer not recognised!\nMake sure it is spelt right and starts with a capital letter."
Choice = input("Pick either 'Card' or 'Coin' : ")
if Choice == "Coin":
r = random.randint(1,2)
if r == 1:
print ("Heads!")
else:
print ("Tails!")
elif Choice == "Card":
card_number = random.randint(1,13)
if card_number == 1:
card_number = "Ace"
elif card_number == 11:
card_number = "Jack"
elif card_number == 12:
card_number = "Queen"
elif card_number == 13:
card_number = "King"
card_suit = random.randint(1,4)
if card_suit == 1:
card_suit = "Hearts"
elif card_suit == 2:
card_suit = "Clubs"
elif card_suit == 3:
card_suit = "Diamonds"
else:
card_suit = "Spades"
print(card_number ,"of" , card_suit)
else:
print(error_message)
while flag:
program()
flag = input('Would you like to run the program again? [yes/no] ') == 'yes'
else:
print ("The program will now terminate.")
I know its messy but it works so now I will focus on shortening it and tidying it up. Thank you
Upvotes: 0
Reputation: 613302
At the outermost level of program()
, you have four separate if
statements. Each one will execute, one after the other. The else
is attached to the final if
.
if Choice == "Card":
print(card_number ,"of" , card_suit)
#else print error message
else:
print(error_message)
When Choice
is Coin
then Choice == "Card"
evaluates false, and so the else
branch is chosen. You mean to have a single if
statement like this:
def program():
error_message = ...
Choice = ...
if Choice == "Coin":
# do all the coin related work
elif Choice == "Card":
# do all the card related work
else:
# handle the error
As an aside, it is stylistically preferred to import
modules once only as the first code in a module.
Upvotes: 1