"""
Demonstrates the use of loops to convert numbers
Decimal to Hex Conversion steps:
Divide the number by 16.
Get the integer quotient for the next iteration.
Get the remainder for the hex digit.
Repeat the steps until the quotient is equal to 0
Algorithm for numeric conversion is at:
https://www.rapidtables.com/convert/number/decimal-to-hex.html
"""
stage.set_background("schoolentrance")
# image from https://foxvalleygirlscodingclub.files.wordpress.com/2017/02/fvgcclogotransparent1.png
logo = codesters.Sprite("FVGCCLogo_56f", -105, 50)
logo.set_size(.2)
# sprite = codesters.Text("text")
coding_club_name = codesters.Text("F\nV\nG\nC\nC", -60, 10)
coding_club_name.set_color("fuchsia")
# image from https://publicdomainvectors.org/photos/gahag-fuser9.png
math_whiz = codesters.Sprite("CodingGirl_1f6", 20, -30)
math_whiz.set_size(.4)
math_whiz.say("Greetings! I am LuLu Looper", 3, "yellow")
math_whiz.say("I can convert decimal numbers to hexadecimal numbers\nusing 2 kinds of loops a while and a for!", 3)
decimal_number = 256
#Exception Handling: because we need numeric input we have to handle the case
# where users input non-numeric data
# Use the try..except code block. If the integer conversion fails, the code will
# go to the except block instead of raising an ugly error message
try:
decimal_number = int(math_whiz.ask("Please give me a number to convert:"))
math_whiz.say("Thanks! I will happily convert " + str(decimal_number) + " to Hex for you", 3)
except:
math_whiz.say("That isn't a number...", 3)
math_whiz.say("I will use my favorite number " + str(decimal_number), 3)
# this function uses 2 types of loops, a while and a for
def decimal_to_hex(dec):
hex_digits = []
quotient = dec
if quotient == 0:
return "00"
#While loops continue to repeat as long as the condition is true
while quotient > 0:
print ("Quotient: " + str(quotient))
remainder = quotient % 16 #modulus operator % gives remainder
hex_digits.append(str(remainder))
print ("Remainder: " + str(remainder))
quotient = quotient/16
hex_digits.reverse() #reverse is a built-in METHOD for stirngs
#hex_digits is a list of string values between 0 and 15
# because each Hex value has to be a single character,
# Hex values between 10 and 15 need to be represented by letters A to F
converted_digits = "" #string will hold the converted Hex result
# this is our second type of loop - iterate through each value in a list
# we can loop throught the values in a list using the for value in list code block:
for value in hex_digits:
if value == "10":
value = "A"
elif value == "11":
value = "B"
elif value == "12":
value = "C"
elif value == "13":
value = "D"
elif value == "14":
value = "E"
elif value == "15":
value = "F"
converted_digits += str(value) #concatenate each value to the result string
return converted_digits
result = decimal_to_hex(decimal_number)
math_whiz.say(str(decimal_number) + " converted to Hexadecimal is: " + result, 5)
math_whiz.say("Try asking me to convert different powers of 16\nlike 16, 256, 4096, 65536, etc. for interesting results", 5)