Reputation: 73
I am trying to make a Heads Or Tails program in Python. I am a newbie and I have just got into Python. What I try to achieve is to have the program pick either Heads or Tails without me knowing it (Yes, import random
, etc.) and I would like to have a single try when guessing. This is what I have achieved so far, yet it is not very close to what i am looking for. Any thoughts? I have tried implementing the different random arguments I found on a Python website but they don't work (such as randint
for integers)... Thanks!
print """
Welcome to our game. This is a heads or tails game and you will be the one who gets to pick a possible answer. Lets begin!
"""
print "~-~-~-~-" * 10
theirGuess = raw_input("Whats your guess? : ")
ourAnswer = "Heads" # For Debugging purposes to check if the program works
notCorrectAnswer = "Tails" # To eliminate the possibility of not being either tails or heads in case of mistaken answer
if theirGuess == ourAnswer:
print "You got it!"
elif theirGuess != notCorrectAnswer and ourAnswer:
print "You didnt get it! Try entering either Tails or Heads!"
else:
print "You didnt get it! Try again next time!"
Upvotes: 2
Views: 520
Reputation: 2240
To make the whole thing continue until exited by user, and including @Baruchel's answer:
import random
print """
Welcome to our game. This is a heads or tails game and you will be the one who gets to pick a possible answer. Lets begin!
"""
cont = 1 #To force the game to run for one round without user input
while(cont == 1): #cont is used to take user choice, whether to run it again or not
print "~-~-~-~-" * 10
theirGuess = raw_input("Whats your guess? : ")
ourAnswer = random.choice(["Heads","Tails"])
if ourAnswer == "Heads":
notCorrectAnswer = "Tails"
else:
notCorrectAnswer = "Heads"
if theirGuess == ourAnswer:
print "You got it!"
elif theirGuess != notCorrectAnswer and ourAnswer:
print "You didnt get it! Try entering either Tails or Heads!"
else:
print "You didnt get it! Try again next time!"
cont = input("Do you want to continue? Press 1 if yes, press 0 if no.: ") #Take user choice on whether to run it again or not
while (cont != 0 and cont != 1): # If user puts in a different number (neither 1 or 0), make them enter until they put a right choice
cont = input("Please try again. Press 1 if yes, press 0 if no.: ")
Upvotes: 0
Reputation: 7517
You should try:
import random
ch = random.choice(["Heads","Tails"])
which will put into the variable ch
either "Heads" or "Tails". Try to do something from that.
Upvotes: 4