0% found this document useful (0 votes)
42 views

Holiday Coding Tasks

The document contains code snippets and explanations for longest substring counting, factorial finding, arithmetic questions, a fruit machine simulation, file reading/writing, and an introduction to HTML. It includes examples of functions, loops, conditionals, inputs, outputs, and basic HTML elements and styling.

Uploaded by

lavishbails
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Holiday Coding Tasks

The document contains code snippets and explanations for longest substring counting, factorial finding, arithmetic questions, a fruit machine simulation, file reading/writing, and an introduction to HTML. It includes examples of functions, loops, conditionals, inputs, outputs, and basic HTML elements and styling.

Uploaded by

lavishbails
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

longest substring

count = 0
arr = ["a","b","c","c"]
string = []

def substring(string,arr,count):
for i in range (len(arr)):
for j in range (len(arr[i])):
if arr[j]!=arr[j+1]:
count==count+1
if arr[j+1]!=arr[j+2]:
count==count+1
string.append(arr[j])
string.append(arr[j+1])
string.append(arr[j+2])
print(string)

substring(string,arr,count)

factorial finder

n = int(input("Enter a number:"))
def f(n):
for i in range (1):
f = n-1
print(n*f)
for j in range (n-1):
f = f-1
print(n*f)

f(n)

Write a program that asks random +, *, -, / questions.

import random
enter = input("Press W to start:")
arr = [1,2,3,4,5,6,7,8,9,10]
w = []
op = ["+","-","/"]
def question(enter):
if enter == "W":
for i in range(8):
n=random.randint(1,16)
w.append(n)
print(n)
computer = random.choice(op)
print(computer)
n2 = random.randint(1,16)
print(n2)
q2 = int(input("What is the answer:"))
if computer == "+":
if q2 != n+n2:
if q2 == True:
count = 1
print("Incorrect lol")
elif computer == "-" and q2 != n-n2:
print("Incorrect lol")
elif computer == "/":
if q2 != n/n2:
print("Incorrect lol")

question(enter)

Arithmetic test

import random

name = input("What is your name:")


Class = input("What class are you in:")

with open("Data.txt","w") as file:

file.write(name)
my_variable = "\n".join(name)
file.write(Class)
my_variable2 = "\n".join(Class)

def questions(name):
score = 0
num1 = random.randint(1, 20)
num2 = random.randint(1, 20)
operator = random.choice(['+', '-', '*', '/'])
question = f"{num1} {operator} {num2}"
value = int(input(question))
answer = eval(question)
if value == answer:
score = score+1
print("Correct")
else:
print("Incorrect")
print(f"Your score is {score}")
for i in range(10):
questions(name)

Fruit Machine

import random
v = input("w:")
array = ["Cherry","Bell","Lemon","Orange", "Star", "Skull"]
def rolls(v):
credit = 1
print("Rolling......")
credit = credit-0.20
for i in range(1):
choice = random.choice(array)
choice2 = random.choice(array)
print(f"Nice you rolled two {choice}")
if choice==choice2:
credit = credit+0.50
print(f"congrats you are now on {credit}")
for i in range(1):
choice = random.choice(array)
choice2 = random.choice(array)
choice3 = random.choice(array)
if choice=arr[1] and choice2 == arr[1] and choice3 == arr[1]:
print(f"jackpot your on {credit}")
print(f"Nice you rolled three {choice}")
if choice==choice2 and choice2 == choice3:
credit = credit+1
print(f"congrats you are now on {credit}")
elif choice==arr[1] and choice2 == arr[1] and choice3 == arr[1]:
print(f"jackpot your on {credit}")
rolls(v)

variable = int(input("Enter a number:"))


arr = []
arr.append(variable)
n = int(input("What number do you want:"))
print(n+variable)

v = input("Would you like to convert it back to a string:")


if v == "yes":
print(arr)
else:
print("Okay")

print(f"You decoded {variable} by adding {n} to it")


introduction to files:
for i in range (3):
fname = input("Enter your firstname:")
lname = input("Enter your lastname:")
num = input("Enter your fav num:")
myfile = open("users.txt","w")
myfile.write(fname+lname+num)
myfile.close()
print(fname[0]+fname[1]+fname[2]+lname[0]+lname[1]+lname[2]+num)

w = input("Enter a name:")
def names(w):
for i in range(6):
name = input("Enter a name:")
if len(name)>=6:
myfile = open("names.txt","w")
myfile.write(name)
myfile.close()
else:
print("Too short")
names(w)

File reading and writing:

arr = [1,2,3]

myfile = open("names.txt","r")
kn = input("Enter a name:")
def sequence(kn):
if kn in myfile:
print(kn,arr[0])
else:
print(kn,arr[+1])

name = input("Enter name:")


age = input("Enter your age:")
name1 = input("Enter name:")
age2 = input("Enter your age:")

myfile = open("names.txt","w")
myfile.write(f"{name}{age}\n{name1}{age2}")

myfile = open("names.txt","r")
print(myfile.read())
myfile.close()

v1 = input("Enter two numbers:")


v2 = input("Enter two numbers:")
if v1>v2:
myfile = open("largest.txt","w")
myfile.write(v1)
myfile.close()
else:
myfile = open("largest.txt","w")
myfile.write(v2)
myfile.close()

MR LEEMAN TASK(semi complete)

fname = input("Enter your firstname:")


lname = input("Enter your lastname:")
num = input("Enter your favourite number:")
variable = fname[0]+fname[1]+fname[2]+lname[0]+lname[1]+lname[2]+num
print("Your username is:",variable)

myfile = open("users.txt","w")
myfile.write(variable)
myfile.close()

def login():
username = input("Enter your username:")
myfile = open("users.txt","r")
if username in myfile:
myfile.close()
myfile = open("users.txt","w")
myfile.write("snowman5")

myfile.close()
myfile = open("users.txt","r")
password = input("Enter your password:")
if password in myfile:
menu=input(f"Welcome {variable} what would you like to do?")
myfile.close()

def sta():
num = input("How many students do you have")
for i in range(len(num)):
name = input("Enter student name:")
ID = input("Enter student ID:")
myfile = open("students.txt","w")
myfile.write(name,"\n")
myfile.write(ID)
myfile.close()
student = input("By enterring an ID you cam view a student:")
myfile = open("students.txt.r")
if student in myfile:
print(myfile)
myfile.close()
sta()

INtroduction to HTML

<html>
<head>
<title>Baileys superstore</title>
<style>
h1{font-family:arial;
}
.customerQoute{background-color:#E8C3E1;
}
document.getElementbyId("intro"){
color:#540c07;
}
</style>
</head>
<body>
<p id = "intro">
Welcome to baileys superstore
<script>
var DarkRed = #540c07
</script>
<h1>Hello World</h1>
<div class = "customerQuote">
</div>
</body>

</html>

Exercise 2

<html>
<head>
<title>The Title</title>
</head>
<body>
<a href "https://www.google.co.uk/" target =_blank title = "Google link">
<button style="font-size:40px; background-color:#064216;color:ffffff; border-
radius:px;border-color:#ff0000" >Google</button>
</a>

<form>
<div>
<label> firstname:</label>
<input type = "text" placeholder = "e.g Diana">
</div>
<br>
<div>
<label> lastname:</label>
<input type = "text" placeholder = "e.g Daylami">
</div>
<br>
<label> username:</label>
<input type = "text" placeholder = "e.g iWatchBratz">
</div>
<br>
<div>
<label> password:</label>
<input type = "text" placeholder = "e.g tallmansofine">
</div>
<br>
<button style = "font-family:arial; background-color:green;border-color:#ff0000
border-radius;45px">Register</button>
</form>
<script>
function validPassword() {
var reset = "Please try again:"
var password = getElementById("password");
var PreferredPassword = password.value;
if (/[A-Z].test(password))
return reset
}
</script>
</body>

</html>

You might also like