0% found this document useful (0 votes)
2 views9 pages

Python Functions and Randomization Guide

The document provides a collection of Python programming concepts and examples, including functions for type checking, string manipulation, random number generation, and list operations. It includes code snippets for a Love Calculator, Password Generator, and Hangman game, demonstrating the use of built-in functions and libraries. Additionally, it covers basic programming constructs such as loops, conditionals, and user input handling.

Uploaded by

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

Python Functions and Randomization Guide

The document provides a collection of Python programming concepts and examples, including functions for type checking, string manipulation, random number generation, and list operations. It includes code snippets for a Love Calculator, Password Generator, and Hangman game, demonstrating the use of built-in functions and libraries. Additionally, it covers basic programming constructs such as loops, conditionals, and user input handling.

Uploaded by

lepa15321997
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

- type()

Funciona para verificar que tipo es, si es un integer o un string, etc.

- str()

Funciona para convertir a string

- f-string

- Love exercise
.count()
.lower()

# 🚨 Don't change the code below 👇


print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")
# 🚨 Don't change the code above 👆

#Write your code below this line 👇

name_1 = [Link]()
name_2 = [Link]()
name = name_1 + name_2

T = [Link]("t")
R = [Link]("r")
U = [Link]("u")
E = [Link]("e")

L = [Link]("l")
O = [Link]("o")
V = [Link]("v")
E = [Link]("e")

true = T + R + U + E
love = L + O + V + E

true1 = str(true)
love1 = str(love)

final = true1 + love1


final1 = int(final)

if final1 < 10 or final1 > 90:


print(f"Your score is {final1}, you go together like coke and me
ntos")
elif 40 < final1 < 50:
print(f"Your score is {final1}, you are alright together")
else:
print(f"Your score is {final1}")

- Random integer
Incluye al 1 y al 10

- Random float
Solo incluye decimales entre el 0 y 1 sin incluirlos
- Append / Extend
Para agregar un nuevo elemento al final de la lista
[Link]

[Link]
numbers-sequences

- Split
Random choice of chosing an element from a list
import random

names_string = input("Give me everybody's names, separated by a comm


a. ")
names = names_string.split(", ")

member = len(names)

num_chosen = [Link](0, member - 1)

print(f"{names[num_chosen]} is going to buy the meal today!")

# easiest way to do it
print([Link](names))

- For number in range()


No include 10
Password exersice
#Password Generator Project
import random

letters = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")


nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

#Eazy Level - Order not randomised:


#e.g. 4 letter, 2 symbol, 2 number = JduE&!91
password = ""

for letter in range(0, nr_letters):


password = password + [Link](letters)

for symbol in range(0, nr_symbols):


password = password + [Link](symbols)

for number in range(0, nr_numbers):


password = password + [Link](numbers)

print(password)

#Hard Level - Order of characters randomised:


#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P

pass_list = []

for letter in range(0, nr_letters):


# pass_list.append([Link](letters))
pass_list += [Link](letters)

for symbol in range(0, nr_symbols):


pass_list += [Link](symbols)

for number in range(0, nr_numbers):


pass_list += [Link](numbers)

[Link](pass_list)
print(pass_list)

acomodado = ""

for acom in pass_list:


acomodado += acom

print(acomodado)

BUILT IN FUNCTION
[Link]
hangman part 1
import random

#TODO-1: - Update the word list to use the 'word_list' from hangman_words.py

#Delete this line: word_list = ["ardvark", "baboon", "camel"]

from hangman_words import word_list

from replit import clear

chosen_word = [Link](word_list)

word_length = len(chosen_word)

end_of_game = False

lives = 6

#TODO-3: - Import the logo from hangman_art.py and print it at the start of the game.

from hangman_art import logo

print(logo)

#Testing code

# print(f'Pssst, the solution is {chosen_word}.')

#Create blanks

display = []

for _ in range(word_length):

display += "_"

while not end_of_game:

guess = input("Guess a letter: ").lower()


clear()

#TODO-4: - If the user has entered a letter they've already guessed, print the letter and let them know.

if guess in display:

print(f"You've already guessed {guess}")

#Check guessed letter

for position in range(word_length):

letter = chosen_word[position]

#print(f"Current position: {position}\n Current letter: {letter}\n Guessed letter: {guess}")

if letter == guess:

display[position] = letter

#Check if user is wrong.

if guess not in chosen_word:

#TODO-5: - If the letter is not in the chosen_word, print out the letter and let them know it's not in
the word.

print(f"You guessed {guess}, that's not in the word. You lose a life.")

lives -= 1

if lives == 0:

end_of_game = True

print("You lose.")

#Join all the elements in the list and turn it into a String.

print(f"{' '.join(display)}")

#Check if user has got all letters.

if "_" not in display:


end_of_game = True

print("You win.")

#TODO-2: - Import the stages from hangman_art.py and make this error go away.

from hangman_art import stages

print(stages[lives])

Def function

You might also like