Imports


#1

How come whenever I import random and try to use random.choice it doesn’t work and says there is no choice attribute or for anything I try attributed to random?


#2

Are you providing an non-empty sequence to choose from?

>>> from random import choice
>>> rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
>>> choice(rainbow)
'blue'
>>> choice(rainbow)
'yellow'
>>> choice(rainbow)
'blue'
>>> choice(rainbow)
'red'
>>> choice(rainbow)
'green'
>>> 

Using from we import a method from the class but it now becomes a standalone function as though it was built in to the standard library. This uses less memory and is more convenient to use. I’m no expert and there may be gotcha’s I don’t know. To me it makes more sense than polluting the namespace with the entire random class, or any other for that matter. Merge the functions your program needs and forget the rest.

import random

Not gonna happen in my code.

from random import *

Also not gonna happen. Why would we need every method of the random class to be standalone functions in our program?

If we need multiple methods to be at our disposal, them import them, and them alone.

from random import randrange, randint, random, choice
>>> rainbow[randrange(len(rainbow))]
'violet'
>>> rainbow[randrange(len(rainbow))]
'blue'
>>> rainbow[randrange(len(rainbow))]
'orange'
>>> 

#3

Thank you this is very helpful


#4

@mtf
Man am I glad people like you are in Codecademy


#5

@mtf
This is what I needed it for.
from random import choice
enemy_pokemon = [‘Bulbasaur’, ‘Ivysaur’, ‘Venusaur’, ‘Charmander’, ‘Charmeleon’, ‘Charizard’, ‘Squirtle’, ‘Wartortle’, ‘Blastoise’, ‘Caterpie’, ‘Metapod’, ‘Butterfree’, ‘Weedle’, ‘Kakuna’, ‘Beedrill’, ‘Pidgey’, ‘Pidgeotto’, ‘Pidgeot’, ‘Rattata’, ‘Raticate’, ‘Spearow’, ‘Fearow’, ‘Ekans’, ‘Arbok’, ‘Pikachu’, ‘Raichu’, ‘Sandshrew’, ‘Sandslash’, ‘Nidoran-female’, ‘Nidorina’, ‘Nidoqueen’, ‘Nidoran-male’, ‘Nidorino’, ‘Nidoking’, ‘Clefairy’, ‘Clefable’, ‘Vulpix’, ‘Ninetales’, ‘Jigglypuff’, ‘Wigglytuff’, ‘Zubat’, ‘Golbat’, ‘Oddish’, ‘Gloom’, ‘Vileploom’, ‘Paras’, ‘Parasect’, ‘Venonat’, ‘Venomoth’, ‘Diglett’, ‘Dugtrio’, ‘Meowth’, ‘Persian’, ‘Psyduck’, ‘Golduck’, ‘Mankey’, ‘Primeape’, ‘Growlithe’, ‘Arcanine’, ‘Poliwag’, ‘Poliwhirl’, ‘Poliwrath’, ‘Abra’, ‘Kadabra’, ‘Alakazam’, ‘Machop’, ‘Machoke’, ‘Machamp’, ‘Bellsprout’, ‘Weepinbell’, ‘Victreebell’, ‘Tentacool’, ‘Tentacruel’, ‘Geodude’, ‘Graveler’, ‘Golem’, ‘Ponyta’, ‘Rapidash’, ‘Slowpoke’, ‘Slowbro’, ‘Magnemite’, ‘Magneton’, ‘Farfetchd’, ‘Doduo’, ‘Dodrio’, ‘Seel’, ‘Dewgong’, ‘Grimer’, ‘Muk’, ‘Shellder’, ‘Cloyster’, ‘Gastly’, ‘Haunter’, ‘Gengar’, ‘Onix’, ‘Drowzee’, ‘Hypno’, ‘Krabby’, ‘Kingler’, ‘Voltorb’, ‘Electrode’, ‘Exeggcute’, ‘Exeggutor’, ‘Cubone’, ‘Marowak’, ‘Hitmonlee’, ‘Hitmonchan’, ‘Lickitung’, ‘Koffing’, ‘Weezing’, ‘Rhyhorn’, ‘Rhydon’, ‘Chansey’, ‘Tangela’, ‘Kangaskhan’, ‘Horsea’, ‘Seadra’, ‘Goldeen’, ‘Seaking’, ‘Staryu’, ‘Starmie’, ‘Mr. Mime’, ‘Scyther’, ‘Jynx’, ‘Electabuzz’, ‘Magmar’, ‘Pinsir’, ‘Tauros’, ‘Magikarp’, ‘Gyarados’, ‘Lapras’, ‘Ditto’, ‘Eevee’, ‘Flareon’, ‘Jolteon’, ‘Vaporeon’, ‘Porygon’, ‘Omanyte’, ‘Omastar’, ‘Kabuto’, ‘Kabutops’, ‘Aerodactyl’, ‘Snorlax’, ‘Articuno’, ‘Zapdos’, ‘Moltres’, ‘Dratini’, ‘Dragonair’, ‘Dragonite’, ‘Mewtwo’, ‘Mew’]
level = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
enemy = choice(enemy_pokemon)
or_random = choice(enemy_pokemon)
critical_hit_chance = [1, 1, 1, 2, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1]
critical_hit = choice(critical_hit_chance)
t = 20
pr = 40
p = 30
pur = 50
rattata_mv_set = [‘Tackle’, ‘Pound’, ‘Play Rough’, ‘Pursuit’]
for key in rattata_mv_set:
rattata_move = choice(rattata_mv_set)
arceus_judgement = 9999999
arceus_hp = 1000
magikarp_hp = 1

cut = 90
enemy_hp = 100
your_hp = 200
question = (“What Pokemon are you using?”)
print question
starter = raw_input()
your_team = [starter]
if starter == “Magikarp”:
yes_no = raw_input(“Are you part of Tyjaes anime gang? y/n”)
if yes_no == “Y”:
print “”"
Arceus lv.100
[|||||||||||||||]

%s lv.1
[|||||||||||||||||||||||||||||||||||||||||||||]""" % (starter)
godkiller = raw_input("""A wild Arceus appeared!!!
It stands no chance.

Splash Unlimited Bladeworks
Judgement Plus Ultra
“”")

if godkiller == "Splash":
  arceus_hp = arceus_hp - splash
  print "%s used Splash!" % (starter)
if arceus_hp <= 0:
  print "Arceus fainted!"
else:
  print "But nothing happened!"

elif godkiller == “Unlimited Bladeworks”:
print “%s used %s!” % (starter, godkiller)
print “Hit %d times” % (blade_works)
arceus_hp = arceus_hp - true_blade
if arceus_hp <= 0:
print “Arceus fainted!”
elif godkiller == “Judgement”:
print “%s used Judgement!” % (starter)
print “But nothing happened!”
print “Arceus: Pathetic.”
print “Arceus used Judgement!”
print “%s fainted” % (starter)
elif godkiller == “Plus Ultra”:
arceus_hp = arceus_hp - Plus_Ultra
print “”"
Magikarp:You may have heard these words before.
But allow me to repeat them for you.

Go beyond, PLUS ULTRA!!!"""
if arceus_hp <= 0:
print “Arceus fainted!”
else:
print “”"How?!?Arceus is still standing

Professor Oak:To be fair Arceus is a God Pokemon."""
judgement = 1
Plus_Ultra = 2000
splash = 666 * 666
blade_works = 999
blade_damage = 10
true_blade = blade_works * blade_damage
elif starter == “Goku”:
print “”"
Frieza lv.100
[|||||||||||||||]

Wait a second, this is supposed to be Pokemon.
Weeb

Goku PwrLv.Over 9000
[Not even his final form]"""
weeb = raw_input("""

Kamehameha Spirit Bomb
Dramatic Screaming Ultra Instinct
“”")
if weeb == “Kamehameha”:
print “KamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehamehameHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”
print “”"
Good job, within the span just 1 season you beat Frieza.That’s a new record!"""
elif weeb ==“Spirit bomb”:
print “”"Goku used Spirit Bomb

Frieza evaded attack!

Goku:NANI!?!

						You Died"""

elif weeb == “Ultra Instinct”:
print “Hit Over 9000 times!”
elif weeb == “Dramatic Screaming”:
print “”"Goku is screaming

But nothing happened!"""
else:
print “Goku used %s” % (weeb)
print “”
print “Frieza:Dafuq?”
else:
print “”"

%s lv.%d
[|||||||||||||||]

%s lv.%d
[|||||||||||||||]
A wild %s appeared!""" % (enemy, level, starter, level, enemy)
your_move = raw_input("""

Cut Pound


“”")

pound = 30
pounded_hp = enemy_hp - pound
if your_move == “Pound”:
enemy_hp = enemy_hp - (pound * critical_hit)
print “%s used Pound!” % (starter)
if critical_hit == 2:
print “Critical hit!!!”
print “%s Health left!!!” % (enemy_hp)
if rattata_move == ‘Tackle’:
your_hp = your_hp - (t * critical_hit)
elif rattata_move == ‘Pound’:
your_hp = your_hp - (p * critical_hit)
elif rattata_move == ‘Play Rough’:
your_hp = your_hp - (pr * critical_hit)
elif rattata_move == ‘Pursuit’:
your_hp = your_hp - (pur * critical_hit)
print “”
print “Rattata used %s!” % (rattata_move)
print “”
if critical_hit == 2:
print “Critical hit!!!”
print “”
print “You have %d health left!” % (your_hp)
elif your_move == “Cut”:
enemy_hp = enemy_hp - (cut * critical_hit)
print “%s used %s” % (starter, your_move)
if critical_hit == 2:
print “”
print “Critical hit!!!”
print “”
print “%s Health left!!!” % (enemy_hp)
if rattata_move == ‘Tackle’:
your_hp = your_hp - (t * critical_hit)
elif rattata_move == ‘Pound’:
your_hp = your_hp - (p * critical_hit)
elif rattata_move == ‘Play Rough’:
your_hp = your_hp - (pr * critical_hit)
elif rattata_move == ‘Pursuit’:
your_hp = your_hp - (pur * critical_hit)
print “”
print “Rattata used %s!” % (rattata_move)
if critical_hit == 2:
print “”
print “Critical hit!!!”
print “”
print “You have %d health left!” % (your_hp)
Gotta_catch_em_all = raw_input(""“Would you like to throw a Pokeball?
Y/N
“””)

if Gotta_catch_em_all == ‘Y’:
if enemy_hp <= 30:
print “”"

Rattata was caught!"""
elif Gotta_catch_em_all == ‘N’:
dew_it = raw_input("""Then finish the job.

Cut Pound


“”")
if dew_it == “Cut”:
print “%s used Cut” % (starter)
print “”
enemy_hp = enemy_hp - (cut * critical_hit)
if critical_hit == 2:
print “”
print “Critical hit!!!”
if enemy_hp <= 0:
print “”"Rattata fainted!

%s gained 30 exp.

%s leveled up to level 6!""" % (starter, starter)
else:
print “You failed”
elif dew_it == “Pound”:
print “%s used Pound”
enemy_hp = enemy_hp - pound
if enemy_hp <= 0:
print “”"Rattata fainted!

%s gained 30 exp

%s leveled up to level 6!""" % (starter, starter)
if enemy_hp <= 0:
print “%s fainted” % (enemy)
elif your_hp <= 0:
print “You lose”


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.