Hacks / Notes 9 & 11
Unit 3.9.1 Hacks
1) It is important to know that algorithms can have different results even though they look the same because it is good to know different ways to accomplish the same thing. This can help visualized the steps and process to make the coding process more efficient.
In addition algorithms having different outputs but similar code is important to know when trouble shooting code in order to find out what is wrong.
isTeams = False
isScore = True
if isTeams == True:
print("You didn't win the game yet!")
else:
if isScore == True:
print("You won!")
else:
print("You didn't win the game yet!")
You won!
isTeams = False
isScore = True
winGame = not(isTeams) and isScore
if winGame == False:
print("You didn't win the game yet!")
if winGame == True:
print("You won the game!")
You won the game!
isRaining = True
temp = 70
if isRaining == True and temp < 45:
print("Wear multiple layers.")
else:
if temp > 45:
print("Wear whatever u want")
else:
if isRaining == False:
bro = input("is it sunny or snowing?")
else:
print("what is the weather like?")
Wear whatever u want
import random
#sets variables for the game
num_guesses = 0
user_guess = 0
upper_bound = 100
lower_bound = 0
#generates a random number
number = random.randint(1,100)
# print(number) #for testing purposes
print(f"I'm thinking of a number between 1 and 100.")
#Write a function that gets a guess from the user using input()
def guess():
return #add something here
#Change the print statements to give feedback on whether the player guessed too high or too low
def search(number, guess):
global lower_bound, upper_bound
if guess < number:
print("You are bad at guessing") #change this
lower_bound = guess
elif guess > number:
print("You suck :(") #change this
upper_bound = guess
return lower_bound, upper_bound
while user_guess != number:
user_guess = guess()
num_guesses += 1
print(f"You guessed {user_guess}.")
lower_bound, upper_bound = search(number, user_guess)
print(f"Guess a number between {lower_bound} and {upper_bound}.")
print(f"You guessed the number in {num_guesses} guesses!")
I'm thinking of a number between 1 and 100.
You guessed None.
num = random.randint(0, 100)
#print(num)
var = False
while var == False:
put = input("I am thinking of a number from between 1-100.")
print(put)
if num < int(put):
print("Guess a lower number")
if int(put) < num:
print("Guess a higher number")
if num == int(put):
print("Good Job, You guessed the right number!")
var = True
50
Guess a higher number
60
Guess a higher number
70
Guess a higher number
80
Guess a higher number
90
Guess a lower number
90
Guess a lower number
85
Guess a lower number
83
Guess a higher number
84
Good Job, You guessed the right number!
index = [12, 14, 43, 57, 79, 80, 99]
mid = int(len(index) / 2)
print(mid)
print(index[mid])
3
57
index = [92, 43, 74, 66, 30, 12, 1]
mid = int(len(index) / 2)
print(mid)
print(index[mid])
3
66
index = [7, 13, 96, 111, 33, 84, 60]
mid = int(len(index) / 2)
print(mid)
print(index[mid])
3
111