0% found this document useful (0 votes)
5 views3 pages

Python Random Tasks With Answers

The document contains a series of Python scripts that demonstrate various applications of the random module. These include guessing a random number, selecting random items from user input, generating random passwords, shuffling names, simulating dice rolls, and more. Each script provides a simple interactive way to engage with randomness in programming.

Uploaded by

22bit12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Python Random Tasks With Answers

The document contains a series of Python scripts that demonstrate various applications of the random module. These include guessing a random number, selecting random items from user input, generating random passwords, shuffling names, simulating dice rolls, and more. Each script provides a simple interactive way to engage with randomness in programming.

Uploaded by

22bit12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Guess the Random Number

import random

number = random.randint(1, 10)

guess = int(input("Guess a number between 1 and 10: "))

if guess == number:

print("Correct!")

else:

print(f"Wrong! The correct number was {number}")

2. Random Choice from User Input

import random

items = input("Enter items separated by commas: ").split(",")

choice = random.choice(items)

print(f"Randomly selected item: {choice.strip()}")

3. Generate a Random Password

import random

characters = input("Enter characters to use in password: ")

length = int(input("Enter desired password length: "))

password = ''.join(random.choice(characters) for _ in range(length))

print(f"Generated password: {password}")

4. Shuffle a List of Names

import random

names = input("Enter names separated by commas: ").split(",")

random.shuffle(names)

print("Shuffled names:", ", ".join(name.strip() for name in names))

5. Simulate Dice Rolls

import random

sides = int(input("Enter number of sides on the dice: "))

rolls = int(input("Enter number of times to roll: "))

for i in range(rolls):
print(f"Roll {i+1}: {random.randint(1, sides)}")

6. Random Lottery Number Generator

import random

count = int(input("How many lottery numbers to generate? "))

max_number = int(input("Maximum number allowed: "))

numbers = random.sample(range(1, max_number + 1), count)

print("Lottery numbers:", numbers)

7. Yes or No Predictor

import random

input("Ask your Yes/No question: ")

print(random.choice(["Yes", "No"]))

8. Random Quote Picker

import random

quotes = input("Enter quotes separated by semicolons: ").split(";")

print("Random Quote:", random.choice(quotes).strip())

9. Simple Math Quiz

import random

a = random.randint(1, 10)

b = random.randint(1, 10)

answer = int(input(f"What is {a} + {b}? "))

if answer == a + b:

print("Correct!")

else:

print(f"Wrong! The correct answer is {a + b}")

10. Random Seating Arrangement

import random

names = input("Enter names separated by commas: ").split(",")

random.shuffle(names)
for i, name in enumerate(names, 1):

print(f"Seat {i}: {name.strip()}")

11. Generate a Random Integer Between 1 and 100

import random

print("Random number between 1 and 100:", random.randint(1, 100))

12. Shuffle a Deck of Cards

import random

suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']

deck = [f"{rank} of {suit}" for suit in suits for rank in ranks]

random.shuffle(deck)

print("Shuffled deck:", deck)

13. Flip a Coin

import random

print("Coin flip result:", random.choice(["Heads", "Tails"]))

14. Roll Two Dice and Show Total

import random

die1 = random.randint(1, 6)

die2 = random.randint(1, 6)

print(f"Die 1: {die1}, Die 2: {die2}, Total: {die1 + die2}")

15. Randomly Pick a Team Leader

import random

names = input("Enter team member names separated by commas: ").split(",")

leader = random.choice(names)

print(f"Team leader: {leader.strip()}")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy