0% found this document useful (0 votes)
6 views12 pages

Python Coding

The document contains a series of Python code snippets that cover various programming concepts such as user input handling, conditional statements, loops, data structures (lists, tuples, sets, dictionaries), and functions. It includes examples for calculating ticket prices based on age, a simple slot machine game, basic arithmetic operations, and string manipulation. Additionally, it demonstrates the creation and usage of functions to encapsulate code logic.

Uploaded by

turi313
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)
6 views12 pages

Python Coding

The document contains a series of Python code snippets that cover various programming concepts such as user input handling, conditional statements, loops, data structures (lists, tuples, sets, dictionaries), and functions. It includes examples for calculating ticket prices based on age, a simple slot machine game, basic arithmetic operations, and string manipulation. Additionally, it demonstrates the creation and usage of functions to encapsulate code logic.

Uploaded by

turi313
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

age = int(input("your age: " ))

has_ticket = True
price = 10.00
if age >= 65:
print('you are a senior citizen')
print(f'price for senior citizen is ${price * 0.5}')
elif age < 18:
print('you are a child')
print(f'price for the ticket for child is {price *0.75}')
else:
print('you are teenager')
if has_ticket:
print('you may enter')
else:
print('you need to buy a ticket')
name = input("Enter your name :")
while name == "":
name = input("Enter your name :")
age = int(input("Enter your age :"))
while age < 0:
print("age can't be less than 0")
age = int(input("Enter your age :"))
print(f"hello {name}!")
print(f"you are {age} years old")
fruits = ["apple", "Banana" , "orange" , "coconumt"]
fruit = input("search for fruit: ")
if fruit in fruits:
print(f"{fruit} is in the list")
else:
print(f"{fruit} is not in the list")
import random

MAX_LINES = 3

MAX_BET = 100

MIN_BET = 1

ROWS = 3

COLS = 3

symbol_count = {

"A": 2,

"B": 4,

"C": 6,

"D": 8

symbol_value = {

"A": 5,

"B": 4,

"C": 3,

"D": 2

}
def check_winnings(columns, lines, bet, values):

winnings = 0

winning_lines = []

for line in range(lines):

symbol = columns[0][line]

for column in columns:

symbol_to_check = column[line]

if symbol != symbol_to_check:

break

else:

winnings += values[symbol] * bet

winning_lines.append(line + 1)

return winnings, winning_lines

def get_slot_machine_spin(rows, cols, symbols):

all_symbols = []

for symbol, symbol_count in [Link]():

for _ in range(symbol_count):

all_symbols.append(symbol)
columns = []

for _ in range(cols):

column = []

current_symbols = all_symbols[:]

for _ in range(rows):

value = [Link](current_symbols)

current_symbols.remove(value)

[Link](value)

[Link](column)

return columns

def print_slot_machine(columns):

for row in range(len(columns[0])):

for i, column in enumerate(columns):

if i != len(columns) - 1:

print(column[row], end=" | ")

else:

print(column[row], end="")
print()

def deposit():

while True:

amount = input("What would you like to deposit? $")

if [Link]():

amount = int(amount)

if amount > 0:

break

else:

print("Amount must be greater than 0.")

else:

print("Please enter a number.")

return amount

def get_number_of_lines():

while True:

lines = input(

"Enter the number of lines to bet on (1-" + str(MAX_LI

if [Link]():
lines = int(lines)

if 1 <= lines <= MAX_LINES:

break

else:

print("Enter a valid number of lines.")

else:

print("Please enter a number.")

return lines

def get_bet():

while True:

amount = input("What would you like to bet on each l

if [Link]():

amount = int(amount)

if MIN_BET <= amount <= MAX_BET:

break

else:

print(f"Amount must be between ${MIN_BET} - ${MAX

else:

print("Please enter a number.")

return amount
def spin(balance):

lines = get_number_of_lines()

while True:

bet = get_bet()

total_bet = bet * lines

if total_bet > balance:

print(

f"You do not have enough to bet that amount, your cu

else:

break

print(

f"You are betting ${bet} on {lines} lines. Total bet is e

slots = get_slot_machine_spin(ROWS, COLS, symbol_c

print_slot_machine(slots)

winnings, winning_lines = check_winnings(slots, lines,

print(f"You won ${winnings}.")

print(f"You won on lines:", *winning_lines)

return winnings - total_bet


def main():

balance = deposit()

while True:

print(f"Current balance is ${balance}")

answer = input("Press enter to play (q to quit).")

if answer == "q":

break

balance += spin(balance)

print(f"You left with ${balance}")

main()

operator = input("Enter an operator (+ - * /): ")


num1 = float(input("Enter 1st number: "))
num2 = float(input("Enter 2nd number: "))
if operator == "+":
result = num1 + num2
print("Result:", result)
elif operator == "-":
result = num1 - num2
print("Result:", result)
elif operator == "*":
result = num1 * num2
print("Result:", result)
elif operator == "/":
result = num1 / num2
print("Result:", result)

#this is for list

drinks = ["coffee", "tea", "liquid"]


dinner = ["biryani", "curry", "mutton"]
dessert = ["cake", "icecream"]
food = [drinks , dinner , dessert]
print(food[1][0])

#tupple

student = ("bro", "21", "male")


print([Link]("bro"))
print([Link]("male"))
for x in student:
print(x)
if "bro" in student:
print("bro is here")

#set

utensil = {"fork", "spoon", "knife", "sticks"}


abc = {"jelly", "jamal", "jaboti", "knife"}
#print([Link](abc))
[Link]("napkin")
[Link]("fork")
[Link]()
for x in utensil:
print(x)
#dictionary = A changeable, unordered collection of unique key: value pairs ,
fast because they use hashing, allow us to access a value quickly

capitals = {'russia': 'moscow', 'iran': 'tehran', 'france': 'paris'}


#print([Link]('russia'))
#print([Link]())
print([Link]())
[Link]({'Germany':'berlin'})
for key, value in [Link]():
print(key,value)

#index operator [] = give access to sequence’s element [str, list, tuples]

name = "dil khan!"


first_name = name[0:3].upper()
last_name = name[4:].upper()
print(first_name)
print(last_name)

#functions = a block of code which is executed when its called

def hello(first_name,last_name, age):


print("hello"+first_name," "+last_name)
print("you are" + str(age) + "years old")
print("have a nice day")
hello("ahmed", "hussain", 21)

#return statement= function send python values/objects back to the


[Link] values/objects are known as the function’s return value.

You might also like