Python Tutorial v3.2.5 Lesson 23.7 - Random Items from a List

preview_player
Показать описание
1:20 Skip the Introduction to Lesson 23.7
8:30 Creating a function using the len(list) and .pop(idx) approach.
18:55 Skip directly to the Lesson 23.7 Challenge Program.

This is an introductory series of Python tutorials. This course, from start to finish, is designed to help someone who has never programmed before learn the basics of coding in Python. As this series continues, we examine more advanced Python techniques, functions, and methods.

Keep in mind this tutorial is using an older version of Python, v3.2.5. You will need to click on the "View Older Releases" button to use this specific version. Using the newer versions will not be an issue at this point, but when the lesson proceeds to basic graphics, the Pygame Module we'll use does not support v3.3+ at this time.
Рекомендации по теме
Комментарии
Автор

The power of YouTube ! This video helped me to do exactly what I've been searching for and it's from 4.5 years ago! Thanks Steve. If you are still checking this Steve, is there a technique for bringing the list back to it's original data after it's been cleared by .pop ?

IdentcoCorp
Автор

Thankyou for the video. Can you please do a tutorial on packing random shapes (polygons for example) without them overlapping please? Cheers

hozad
Автор

if i have a list: 1, 2, 4, 5, 8, 13, 20, 25, 30

how could you code it to: count how many numbers are between 1 and 20?

i would like the answer to be 7 if you include the 1 and 20.

watchandpray
Автор

Hi Steve, sorry for asking a question a video from 2014. How would I make it so the program automatically gives me a random item from a list, without me having to summon it. Basically what I'm doing is a states and capitals memorization game. The user is randomly given a state from a list, and the user must input the corresponding capital, and receives a point if it is correct.

ClutchRocketLeague
Автор

Hi Steve. Thanks for this tutorial. I am struggling to pick out a random item from a list of 9 items and replace it with item number 10 of that list. Hope that makes sense? Thanks in advance.

civcinfo
Автор

Thanks for the tutorial. I want to make a short random list, but I dont wan the .pop( ) at the end, and want the list to keep repeating itself in a random way. What should I do?

yarisolmateo
Автор

Hello, I currently used your advice to help me for a performance test in comp. sci. I would like to know if it is ok to cite this source of yours? I have to since it is a requirement to cite my sources and this has helped me a lot in my program. If wanted, I shall post apart of my code that creates the part you have helped me with.

jalencurtis
Автор

So that's my program

import random
import time

f_city=input("Enter the name of the 1 city\n:")
s_city=input("\nEnter the name of the 2 city\n:")
t_city=input("\nEnter the name of the 3 city\n:")

f_mascot=input("\nEnter the mascot №1\n:")
s_mascot=input("\nEnter the mascot №2\n:")
t_mascot=input("\nEnter the mascot №3\n:")

mascots=[f_mascot, s_mascot, t_mascot]
cities=[f_city, s_city, t_city]

def create():
if len(mascots)>0 and len(cities)>0:
num_m=len(mascots)-1
num_c=len(cities)-1
index=random.randint(0, 2)
f_mascot=mascots.pop(index)
f_cities=cities.pop(index)
index_f=random.randint(0, 1)
s_mascot=mascots.pop(index_f)
index_s=random.randint(0, 1)
s_cities=cities.pop(index_s)
t_mascot=mascots.pop(0)
t_cities=cities.pop(0)
print("Randomizing all the
time.sleep(2)
print("The first team in the league is %s %s"%(f_mascot, f_cities))
print("The first team in the league is %s %s"%(s_mascot, s_cities))
print("The first team in the league is %s %s"%(t_mascot, t_cities))
create()

alexanderstorozhenko
visit shbcf.ru