Unit 3.14 & 3.15 Hacks
import math
decimalNum = float(input("Enter a decimal number to be rounded"))
onlyDecimal = decimalNum - math.floor(decimalNum)
if onlyDecimal >= 0.5 and onlyDecimal < 1:
print(decimalNum, "rounds up to", math.ceil(decimalNum))
elif onlyDecimal > 0:
print(decimalNum, "rounds down to", math.floor(decimalNum))
else:
print(decimalNum, "stays the same")
My code is asking the user to input a number to be rounded. I used the import math in order to do the rounding. I first floor() the original number and subtracted it from the original number to only get the decimals. Then I compared the decimal to see if I need to round up, down, or stay the same. If it is round up, I use the ceil() of the original number to round up. If it is round down, I use the floor() of the original number to round down.
import random
print("Random Number:")
print(random.randint(1,100))
- The import random function randomizes stuff. Random number would return a random number between the two numbers in the range given.
- You can import math and flask and sys
import random
spinnerLand = random.randint(1,8)
if spinnerLand <= 3:
print("You got green")
elif spinnerLand <= 5:
print("You got blue")
elif spinnerLand <= 6:
print("You got purple")
elif spinnerLand <= 7:
print("You got red")
elif spinnerLand <= 8:
print("You got orange")
The numbers from RANDOM(12,20) that can be outputted are 12, 13, 14, 15, 16, 17, 18, 19, 20. All other numbers are excluded