Title
- title: Unit 3 Sections 5-7 Classwork
- toc: true
- comments: true
- categories: [week 13]
# = ; (idk what this does so I just commented it)
sibnum = int(input('How many siblings do you have?'))
if (sibnum > 3):
print("Wow, that is a lot");
else:
print("That is not very many");
gpa = int(input('What is your SAT score?'))
if (gpa > 1051):
print("Above average");
else:
print("Below average");
name = str(input('What is your first name?'))
if (len(name) >= 9):
print("That's long");
else:
print("Not too long");
Below is an example of decimal number to binary converter which you can use as a starting template.
def DecimalToBinary(num):
strs = ""
while num:
# if (num & 1) = 1
if (num & 1):
strs += "1"
# if (num & 1) = 0
else:
strs += "0"
# right shift by 1
num >>= 1
return strs
# function to reverse the string
def reverse(strs):
print(strs[::-1])
# Driver Code
num = 67
print("Binary of num 67 is:", end=" ")
reverse(DecimalToBinary(num))
Below is my work
def convert(n):
if n == 0:
print("0")
elif n == 1:
print("1")
else:
binary = "" # define a string "" variable for storing binary digits; calculated below
# divide the number (n) by 2, until it becomes 0, and store the remainder in the above string variable
while int(n) > 0:
binary += str(i]nt(n%2)) # retreive the remainder, and keep appending to the string
n = n / 2 # change the number to its quotient, then repeat
print(binary[::-1]) # now the binary string has all the desired digits, but in reverse order
# :: is how to reverse string
n = int(input('Enter a number'))
convert(n) # perform the function
import random
def randomnum(): # function for generating random int
n = random.randint(0,255)
print("The random decimal number generated is " + str(n))
return n
def converttobin(n):
if n == 0:
print("00000000")
return "00000000"
elif n == 1:
print("00000001")
return "00000001"
else:
binary = "" # define a string "" variable for storing binary digits; calculated below
# divide the number (n) by 2, until it becomes 0, and store the remainder in the above string variable
while int(n) > 0:
binary += str(int(n%2)) # retreive the remainder, and keep appending to the string
n = n / 2 # change the number to its quotient, then repeat
print(binary[::-1]) # now the binary string has all the desired digits, but in reverse order
# :: is how to reverse string
return binary
def survivors(binary):
survivorstatus = ["Jiya", "Shruthi", "Noor", "Ananya" , "Peter Parker", "Andrew Garfield", "Tom Holland", "Tobey Maguire"]
idx = 0
for bit in binary:
if (bit == "0"):
print(survivorstatus[idx] + " is a ZOMBIE!!!")
else:
print(survivorstatus[idx] + " survived!!!")
idx += 1 # advancing the index to the next position
survivors(converttobin(randomnum())) # perform all the functions