Reputation: 391
So we have a school project to make a program that asks the user whether to pick a random card from a 52 deck or simulate a coin toss - heads or tails. I've got a beta version working but I'm looking into how to make the program more efficient. Basically, the program sets the tuples 'card' and 'coin' to all the possible types, so for coin, heads and tails, and card, the 52 different cards.
The following part of the program sets the variable 'choice' to either card or coin
while choice!='card' and choice!='coin':
choice=input("Pick a card or toss a coin (card/coin)?: ")
if choice!="card" and choice!="coin":
print("Please enter either 'card' or 'coin'...")
time.sleep(2)
Next I want to be able to return a value from either tuple depending on the value of 'choice' without using and if statement, something along the following:
print("The ",choice," picked at random is... ",random.choice("Either card, or coin"),"!")
How would I do this? Thanks, Brandon.
Full program:
#importing modules used in program
import time
import random
#creating duples for possible cards and coins
cards=["Ace of spades","King of spades","Queen of spades","Jack of spades","10 of spades","9 of spades","8 of spades","7 of spades","6 of spades","5 of spades","4 of spades","3 of spades","2 of spades","Ace of diamonds","King of diamonds","Queen of diamonds","Jack of diamonds","10 of diamonds","9 of diamonds","8 of diamonds","7 of diamonds","6 of diamonds","5 of diamonds","4 of diamonds","3 of diamonds","2 of diamonds","Ace of clubs","King of clubs","Queen of clubs","Jack of clubs","10 of clubs","9 of clubs","8 of clubs","7 of clubs","6 of clubs","5 of clubs","4 of clubs","3 of clubs","2 of clubs","Ace of hearts","King of hearts","Queen of hearts","Jack of hearts","10 of hearts","9 of hearts","8 of hearts","7 of hearts","6 of hearts","5 of hearts","4 of hearts","3 of hearts","2 of hearts"]
coin=["Heads","Tails"]
#setting variable 'repeat' to 'y' to start the mainloop of the program
repeat="y"
while repeat=="y":
#blanking the repeat and choice variables so they can be reset later
repeat=""
choice=""
#won't allow the user to continue until either 'card' or 'coin' is entered
while choice!='card' and choice!='coin':
choice=input("Pick a card or toss a coin (card/coin)?: ")
if choice!="card" and choice!="coin":
print("Please enter either 'card' or 'coin'...")
time.sleep(2)
print("The ",choice," picked at random is... ",random.choice(card),"!")
#won't allow the user to continue until either 'y' or 'n' is entered
while repeat!="y" and repeat!="n":
repeat=input("Go again..? (y/n): ")
if repeat!="y" and repeat!="n":
print("Please enter either 'y' for yes or 'n' for no...")
time.sleep(2)
#ending of the program
print("Goodbye!")
time.sleep(2)
Upvotes: 0
Views: 192
Reputation: 8759
I'm for sure if this is what you're asking, but if the tuple you're talking about is this ('coin', 'card')
, then you can do the following:
import random
test = ('coin', 'card')
test[random.randint(0, 1)]
That will randomly select one of the two choices.
Upvotes: 0
Reputation: 531285
The easiest way to accomplish this is to have a dictionary of tuples:
items = { 'coin': coin,
'card': cards }
Then, after choice
has been set properly by your while
loop, use the word provided by the user as the key to select from the dictionary the correct list, which can then be passed to random.choice
:
print("The ", choice, " picked at random is ...", random.choice(items[choice]))
Upvotes: 2