i keep getting attribute error randint - python

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)

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])

Not getting program output in python IDLE

I'm trying to learn python and currently have run the code below but I get no output in the IDLE. What's wrong?
import random
def getAnswer(answerNumber):
if answerNumber == 1:
return 'Its certain'
elif answerNumber == 2:
return 'It is decidedly so'
elif answerNumber == 3:
return 'Yes'
elif answerNumber == 4:
return 'Reply hazy try again'
elif answerNumber == 5:
return 'Ask again later'
elif answerNumber == 6:
return 'Concentrate and ask again'
elif answerNumber == 7:
return 'My reply is no'
elif answerNumber == 8:
return 'Outlook not so good.'
elif answerNumber == 9:
return 'Very doubtful'
r = random.randint(1,9)
fortune = getAnswer(r)
print(fortune)
Your code will be fine if you correct the typo and indentation as they mentioned in comments.
Corrected code
import random
def getAnswer(answerNumber):
if answerNumber == 1:
return 'Its certain'
elif answerNumber == 2:
return 'It is decidedly so'
elif answerNumber == 3:
return 'Yes'
elif answerNumber == 4:
return 'Reply hazy try again'
elif answerNumber == 5:
return 'Ask again later'
elif answerNumber == 6:
return 'Concentrate and ask again'
elif answerNumber == 7:
return 'My reply is no'
elif answerNumber == 8:
return 'Outlook not so good.'
elif answerNumber == 9:
return 'Very doubtful'
r = random.randint(1,9)
fortune = getAnswer(r)
print(fortune)

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.

Solitaire 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.

Categories

Resources