Solitaire python - python

I have code here for moving a card from the deck to the foundation pile. I've imported the necessary details, etc. My problem is, it's too long. Is there any way to make it shorter? How? Thanks :)
def dtof():
suit = raw_input("enter suit: ")
v = trash.pop()
if suit == "D":
if card.suitNumber[v.suit] == 1:
if card.rankNumber[v.rank] == 0:
Diamond.append(v)
elif card.rankNumber[v.rank] == card.rankNumber[Diamond[-1].rank] + 1:
Diamond.append(v)
else:
trash.append(v)
return Diamond[-1]
else:
trash.append(v)
elif suit == "H":
if card.suitNumber[v.suit] == 2:
if card.rankNumber[v.rank] == 0:
Heart.append(v)
elif card.rankNumber[v.rank] == card.rankNumber[Heart[-1].rank] + 1:
Heart.append(v)
else:
trash.append(v)
return Heart[-1]
else:
trash.append(v)
elif suit == "C":
if card.suitNumber[v.suit] == 4:
if card.rankNumber[v.rank] == 0:
Clubs.append(v)
elif card.rankNumber[v.rank] == card.rankNumber[Clubs[-1].rank] + 1:
Clubs.append(v)
else:
trash.append(v)
return Clubs[-1]
else:
trash.append(v)
elif suit == "S":
if card.suitNumber[v.suit] == 3:
if card.rankNumber[v.rank] == 0:
Spade.append(v)
elif card.rankNumber[v.rank] == card.rankNumber[Spade[-1].rank] + 1:
Spade.append(v)
else:
trash.append(v)
return Spade[-1]
else:
trash.append(v)
else:
trash.append(v)

Consider merging Diamond, Heart, Clubs and Spade into a single dictionary, with the key being suit.

Related

how to elimate the need for a large number of if elif else statements

My script uses a if elif else check to determine which array item to look at for crafting values. The issue is there are a total of 24 elif statements to allow for a total of 26 choices, (1 for the if, 1 for error catching). What I'm trying to figure out is how to reduce the number of elif statements so the code is better structured.
while choice == 0:
choice = input("Choose a block (Enter only the Number):")
if not choice.isalpha(): # Makes sure that the input is a number and not a string.
choice = int(choice)
else:
choice = 0
print("Thats not a number. Choose a number Numbnuts.")
if choice == 1:
print("\n",block[0])
elif choice == 2:
print("\n",block[1])
elif choice == 3:
print("\n",block[2])
elif choice == 4:
print("\n",block[6])
elif choice == 5:
print("\n",block[7])
elif choice == 6:
print("\n",block[8])
elif choice == 7:
print("\n",block[3])
elif choice == 8:
print("\n",block[4])
elif choice == 9:
print("\n",block[5])
elif choice == 10:
print("\n",block[9])
elif choice == 11:
print("\n", block[10])
elif choice == 12:
print("\n", block[11])
elif choice == 13:
print("\n",block[12])
elif choice == 14:
print("\n",block[13])
elif choice == 15:
print("\n",block[14])
elif choice == 16:
print("\n",block[15])
elif choice == 17:
print("\n",block[16])
elif choice == 18:
print("\n",block[17])
elif choice == 19:
print("\n",block[18])
elif choice == 20:
print("\n",block[19])
elif choice == 21:
print("\n",block[20])
elif choice == 22:
print("\n", block[21])
elif choice == 23:
print("\n", block[22])
elif choice == 24:
print("\n",block[23])
elif choice == 25:
print("\n",block[24])
elif choice == 26:
print("\n",block[25])
Look for structure:
elif choice == 12:
print("\n", block[11])
elif choice == 13:
print("\n",block[12])
elif choice == 14:
print("\n",block[13])
elif choice == 15:
print("\n",block[14])
elif choice == 16:
print("\n",block[15])
Clearly, this is the same as;
print("\n", block[choice - 1])
However, for choice in {4,5,6,7,8,9} the logic isn't that simple, so you could keep the ifs:
if choice == 4:
print("\n",block[6])
elif choice == 5:
print("\n",block[7])
elif choice == 6:
print("\n",block[8])
elif choice == 7:
print("\n",block[3])
elif choice == 8:
print("\n",block[4])
elif choice == 9:
print("\n",block[5])
else:
print("\n", block[choice - 1])
You could also go a step deeper: for choice in {4,5,6} the index is choice-2, and for choice in {7,8,9} the index is choice-4:
if choice in {4,5,6}:
idx = choice - 2
elif choice in {7,8,9}:
idx = choice - 4
else:
idx = choice - 1
print("\n", block[idx])
As another answer suggested, look for structure; you have different ranges of choices that correspond to different offsets in block, so you can iterate over those:
while True:
try:
choice = input("Choose a block (Enter only the Number):")
except ValueError:
print("Thats not a number. Choose a number Numbnuts.")
continue
if choice < 1 or choice > 26:
continue
print("\n")
for limit, offset in ((3, -1), (6, 2), (9, -4), (26, -1)):
if choice <= limit:
print(block[choice + offset])
break
break
Hence for 1 <= choice <= 3 you print block[choice-1], for 4 <= choice <= 6 you print block[choice+2], and so on.
my version:
block_2 = block[:3]+block[6:9]+block[3:6]+block[9:]
print("\n", block_2[choice - 1])

iterate over pd dataframe to change integers to strings [duplicate]

This question already has answers here:
Remap values in pandas column with a dict, preserve NaNs
(11 answers)
Closed 4 years ago.
It's a very basic question but somehow my loop does not work. I have a pandas dataframe with two columns; path and word. A neural network predicted the outcome value in word, however this is still an integer. I wrote a for loop to replace those integers with words but there are no changes in the df. My df:
path word
0 f7f172c01bec6a3e9a36dcafdf9e02e7df3522e4.wav 21
1 c17c29e392c57a9243ed52175568b40c04912194.wav 21
2 eea9239d4986b1f7dbffdcce76e0fce6e5f38ca8.wav 21
3 4fec4b033ba19d1ac875c0c062fda2869dbece73.wav 21
my loop:
for i in df['word']:
if i == 0:
i == "backward"
elif i == 1:
i == "bed"
elif i == 2:
i == "bird"
elif i == 3:
i == "cat"
elif i == 4:
i == "dog"
elif i == 5:
i == "down"
elif i == 6:
i == "eight"
elif i == 7:
i == "five"
elif i == 8:
i == "follow"
elif i == 9:
i == "forward"
elif i == 10:
i == "four"
elif i == 11:
i == "go"
elif i == 12:
i == "happy"
elif i == 13:
i == "house"
elif i == 14:
i == "learn"
elif i == 15:
i == "left"
elif i == 16:
i == "marvin"
elif i == 17:
i == "nine"
elif i == 18:
i == "no"
elif i == 19:
i == "off"
elif i == 20:
i == "on"
elif i == 21:
i == "one"
elif i == 22:
i == "right"
elif i == 23:
i == "seven"
elif i == 24:
i == "sheilla"
elif i == 25:
i == "six"
elif i == 26:
i == "stop"
elif i == 27:
i == "three"
elif i == 28:
i == "tree"
elif i == 29:
i == "two"
elif i == 30:
i == "up"
elif i == 31:
i == "visual"
elif i == 32:
i == "wow"
elif i == 33:
i == "yes"
elif i == 34:
i == "zero"
You can use pd.Series.map:
#Add the rest of your mapping here (I just included a few)
mapping = {0: 'backward', 1: 'bed', 21: 'one', 22: 'right'}
df['word'] = df['word'].map(mapping)
Returns:
path word
0 f7f172c01bec6a3e9a36dcafdf9e02e7df3522e4.wav one
1 c17c29e392c57a9243ed52175568b40c04912194.wav one
2 eea9239d4986b1f7dbffdcce76e0fce6e5f38ca8.wav one
3 4fec4b033ba19d1ac875c0c062fda2869dbece73.wav one

B and C aren't working (Python3)

I'm quite confused at why the option "B" or "C" doesn't work. You are supposed to be able to navigate the story by choosing any of the 3 options. The only option that works is "A". It's probably the smallest thing I have overlooked.
Click to See the Code Window
The program executes (From the Fuctions.py, SplashScreens()):
...
print(SplashScreen.Splash_Screen19)
cls()
Story_1_to_4(Text.Story_1,2,3,4)
Which runs this... (Located in Functions.py){Through = True}{Key = False}
def Story_1_to_4(Story_Num, Path1, Path2, Path3):
global Through
global Key
if Path1 == 3 and Path2 == 4 and Path3 == 10:
Key = True
while Through == True:
Choice = input(Story_Num)
if Choice == "A":
Choice = Path1
Through = False
elif Choice == "B":
Choice = Path2
Through == False
elif Choice == "C":
Choice = Path3
Through == False
else:
cls()
print(Text.Invalid_Syntax)
time.sleep(2)
cls()
ResetThrough()
Decision(Choice)
Story_1: (From Text.py)
Image of the Variable Story_1
And then Decision is... (Located in Functions.py)
def Decision(Choice):
cls()
if Choice == 1:
Story_1_to_4(Text.Story_1,2,3,4)
elif Choice == 2:
Story_1_to_4(Text.Story_2,3,4,10)
elif Choice == 3:
Story_1_to_4(Text.Story_3,5,6,4)
elif Choice == 4:
Story_1_to_4(Text.Story_4,7,8,9)
elif Choice == 5:
Story_Ending(Text.Story_5)
elif Choice == 6:
Story_Ending(Text.Story_6)
elif Choice == 7 and Key == True:
Story_Ending(Text.Story_7_With_Key)
elif Choice == 7 and Key == False:
Story_Ending(Text.Story_7_Without_Key)
elif Choice == 8:
Story_Ending(Text.Story_8)
elif Choice == 9:
Story_Ending(Text.Story_9)
elif Choice == 10:
Story_Ending(Text.Story_10)
For A, you set Through = False. For B and C you wrote Through == False which just evaluates an expression but doesn't assign Through.

List failing on second run

I am currently trying to program a mathematical card trick, which asks the user what pile their random card is in. However, the second time it runs (the 'trick phase' has to occur 3 times for the trick to work) the list index becomes out of range. I am unsure where exactly the problem lies and will attach the current version so you can try to run it to see clearer. Thanks!
import random
def makedeck():
listy = []
cardsindeck = 0
while cardsindeck != 21:
suit = random.randint(1,4)
if suit == 1:
suit = "D"
elif suit == 2:
suit = "H"
elif suit == 3:
suit = "S"
else:
suit = "C"
cardtype = random.randint(1,13)
if cardtype == 1:
card = "A"
elif cardtype == 2:
card = "2"
elif cardtype == 3:
card = "3"
elif cardtype == 4:
card = "4"
elif cardtype == 5:
card = "5"
elif cardtype == 6:
card = "6"
elif cardtype == 7:
card = "7"
elif cardtype == 8:
card = "8"
elif cardtype == 9:
card = "9"
elif cardtype == 10:
card = "10"
elif cardtype == 11:
card = "J"
elif cardtype == 12:
card = "Q"
else:
card = "K"
cardandsuit = (card + suit)
if cardandsuit not in listy:
listy.append(cardandsuit)
cardsindeck = cardsindeck + 1
return listy
def dealdeck(listy):
list1 = []
list2 = []
list3 = []
for i in range(len(listy)):
if i % 3 == 0:
list1.append(listy[i])
elif i % 3 == 1:
list2.append(listy[i])
else:
list3.append(listy[i])
return[list1, list2, list3]
def makepiles(pile1,pile2,pile3):
print("Pile 1\t\tPile 2\t\t Pile 3\t\t")
for i in range(7):
print(pile1[i],"\t\t",pile2[i],"\t\t",pile3[i],"\t\t")
def usercardpile():
userinput = input("What pile is your card in?")
if userinput == "1" or userinput.title() == "One":
return 1
elif userinput == "2" or userinput.title() == "Two":
return 2
elif userinput == "3" or userinput.title() == "Three":
return 3
else:
print("Please only write 1, 2 or 3")
return usercardpile()
listy = makedeck()
pile1, pile2, pile3 = dealdeck(listy)
for i in range(1,4):
newlisty = makepiles(pile1,pile2,pile3)
userspile = usercardpile()
if userspile == 1:
newlisty = (pile2,pile1,pile3)
elif userspile == 2:
newlisty = (pile1,pile2,pile3)
else:
newlisty = (pile1,pile3,pile2)
pile1, pile2, pile3 = dealdeck(newlisty)
print("Your card is",newlisty[10])
One issue is with this line of code:
newlisty = makepiles(pile1, pile2, pile3)
You're expecting makepiles to return 3 lists, whereas it returns None (no explicit item returned).
I imagine if you were to return the piles from that function, it'd work.
The other thing is, You are doing this:
newlisty = (pileX, pileY, pileZ)
This will create a tuple of lists, and you will iterate over the entire lists rather than the individual cards. I believe you want
newlisty = pile1 + pile3 + pile2`
This'll create a composite list of 21 elements by linearly combining the smaller piles.
Other comments:
Consider storing your decktype and card type in dicts. That way, you can quickly lookup and generate your piles without having to write a long set of if statements. Example:
You can reduce
cardtype = random.randint(1,13)
if cardtype == 1:
card = "A"
elif cardtype == 2:
card = "2"
elif cardtype == 3:
card = "3"
elif cardtype == 4:
card = "4"
elif cardtype == 5:
card = "5"
elif cardtype == 6:
card = "6"
elif cardtype == 7:
card = "7"
elif cardtype == 8:
card = "8"
elif cardtype == 9:
card = "9"
elif cardtype == 10:
card = "10"
elif cardtype == 11:
card = "J"
elif cardtype == 12:
card = "Q"
else:
card = "K"
To...
cardtype_lookup = { 1 : 'A', 2 : '2', 3 : '3', .... 12 : 'K' }
card = cardtype_lookup[random.randint(1, 13)]
...And so on.
I think you are hitting a few issues with your code beyond just the final iterations. Below are some suggestions along with comments that I believe accomplishes your purpose.
from random import shuffle
# Function for generating 21 random cards
def makedeck():
# Create 52 cards by suit and cardtype
listy = [card + suit for card in ['A','2','3','4','5','6','7','8','9','10','J','Q','K'] for suit in ['D','H','S','C']]
# Shuffle the list
shuffle(listy)
# Choose only the first 21 items of that list
listy = listy[:21]
return listy
# Function for dividing 21-card deck into 3 equally-sized piles
def dealdeck(listy):
# Iterate over listy, stepping by 3, starting with the first, second, and third item
list1 = listy[::3]
list2 = listy[1::3]
list3 = listy[2::3]
# Return the three lists as three items to correspond to the three piles of the call
return list1, list2, list3
# This works
def makepiles(pile1,pile2,pile3):
print("Pile 1\t\tPile 2\t\t Pile 3\t\t")
for i in range(7):
print(pile1[i],"\t\t",pile2[i],"\t\t",pile3[i],"\t\t")
# This works
def usercardpile():
userinput = input("What pile is your card in?")
if userinput == "1" or userinput.title() == "One":
return 1
elif userinput == "2" or userinput.title() == "Two":
return 2
elif userinput == "3" or userinput.title() == "Three":
return 3
else:
print("Please only write 1, 2 or 3")
return usercardpile()
listy = makedeck()
pile1, pile2, pile3 = dealdeck(listy)
for i in range(1,4):
# Because this function does not return anything, it should be run on its own, and not assigned to a variable
makepiles(pile1,pile2,pile3)
userspile = usercardpile()
# Now you want to re-order the piles based on user input. Because these are lists, you can simply add them together in a new order to create a newly arranged list
if userspile == 1:
newlisty = pile2 + pile1 + pile3
elif userspile == 2:
newlisty = pile1 + pile2 + pile3
else:
newlisty = pile1 + pile3 + pile2
# Now you create new piles based on the re-sorted list and re-iterate
pile1, pile2, pile3 = dealdeck(newlisty)
# Uses .format method instead
print("Your card is {}".format(newlisty[10]))

i keep getting attribute error randint

everytime I run my code I get attribute error and int has no randint, but when I looked online how to do random, that is what it told me to do, please help.
def gorandom():
if random.randint(1,8) == 1:
turtle.goto(-250,250)
elif random.randint(1,8) == 2:
turtle.goto(0,250)
elif random.randint(1,8) == 3:
turtle.goto(250,250)
elif random.randint(1,8) == 4:
turtle.goto(250,0)
elif random.randint(1,8) == 5:
turtle.goto(250,-250)
elif random.randint(1,8) == 6:
turtle.goto(0,-250)
elif random.randint(1,8) == 7:
turtle.goto(-250,-250)
else:
turtle.goto(-250,0)
You are missing an import. Please add
import random
To the top of your file.
try this:
import random
def gorandom():
if random.randint(1,8) == 1:
turtle.goto(-250,250)
elif random.randint(1,8) == 2:
turtle.goto(0,250)
elif random.randint(1,8) == 3:
turtle.goto(250,250)
elif random.randint(1,8) == 4:
turtle.goto(250,0)
elif random.randint(1,8) == 5:
turtle.goto(250,-250)
elif random.randint(1,8) == 6:
turtle.goto(0,-250)
elif random.randint(1,8) == 7:
turtle.goto(-250,-250)
else:
turtle.goto(-250,0)
And be sure your goto(x,y) function works ;)
found solution, thanks guys for help
from random import randint
def gorandom():
if randint(1,8) == 1:
turtle.goto(-250,250)
elif randint(1,8) == 2:
turtle.goto(0,250)
elif randint(1,8) == 3:
turtle.goto(250,250)
elif randint(1,8) == 4:
turtle.goto(250,0)
elif randint(1,8) == 5:
turtle.goto(250,-250)
elif randint(1,8) == 6:
turtle.goto(0,-250)
elif randint(1,8) == 7:
turtle.goto(-250,-250)
else:
turtle.goto(-250,0)

Categories

Resources