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)
Related
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])
This has been a head twister, here is what I am trying to do
Function1: Calls an API for an integer
Function2: Based on the API result, it will play a ffmpeg radio station
This works on its own, the problem happens when the integers changes
This is how it should work
API = 1 -> Play Radio 1 -> API = 1 -> Continue Playing Radio 1 -> API = 2 -> Play Radio 2
My main:
def currentlyPlaying(num):
if num == 1:
musicPlay = subprocess.call(["ffplay", "-nodisp", "http://n0e.radiojar.com/8s5u5tpdtwzuv?rj-ttl=5&rj-tok=AAABf8pZIfoAj4LUj-sLh_n6Vw"])
if num == 2:
musicPlay =subprocess.call(["ffplay", "-nodisp", "http://livstream.xyz:8220/radio.mp3"])
if num == 3:
musicPlay =subprocess.call(["ffplay", "-nodisp", "http://livstream.xyz:8050/radio.mp3"])
if num == 4:
musicPlay =subprocess.call(["ffplay", "-nodisp", "http://stream.zeno.fm/wcue1s1g6ehvv"])
if num == 5:
musicPlay =subprocess.call(["ffplay", "-nodisp", "http://livstream.xyz:8030/radio.mp3"])
if num ==6:
musicPlay =subprocess.call(["ffplay", "-nodisp", "http://livstream.xyz:8190/radio.mp3"])
if num == 7:
musicPlay =subprocess.call(["ffplay", "-nodisp", "https://server03.quran.com.kw:7000/;*.mp3"])
else:
print("Invalid Choice")
def kill(self):
sys.exit()
def mainFunction():
tempTrack = 1
while True:
response = requests.get("http://127.0.0.1:8000/currentTrack")
currentTrack = int(response.text)
time.sleep(1)
if tempTrack != currentTrack:
print("CHANGED")
#os.kill(os.getppid(), signal.SIGTERM)
tempTrack = currentTrack
if currentTrack == 0:
print("Fucker")
elif currentTrack == 1:
print("\n Playing Track: ", currentTrack)
#currentlyPlaying(1)
elif currentTrack == 2:
print("\n Playing Track: ", currentTrack)
#currentlyPlaying(2)
elif currentTrack == 3:
print("\n Playing Track: ", currentTrack)
#currentlyPlaying(3)
elif currentTrack == 4:
print("\n Playing Track: ", currentTrack)
#currentlyPlaying(4)
elif currentTrack == 5:
print("\n Playing Track: ", currentTrack)
#currentlyPlaying(5)
elif currentTrack == 6:
print("\n Playing Track: ", currentTrack)
#currentlyPlaying(6)
elif currentTrack == 7:
print("\n Playing Track: ", currentTrack)
#currentlyPlaying(7)
elif currentTrack == 44:
kill()
else:
print("Invalid, try again")
Main is constantly checking API value, what I need it to do is continue checking API value, even after a radio is playing, and kill the radio and start a new one if a different API number is passed
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
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)
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.