Notes - Wednesday 1/6/21 - Backup of the Temperature Converter Code
1/6/21: My first Python Code. It converts temperatures from F to C and C to F.
Huge thanks for @Tokugero and @Leeoku for helping me. And Girlshark for the inspiration
Code with comments
# Instantiate initial true flag to enter loop
run_loop = True
# Set global variable to count
retry = 0
# Runs code while retry is under 3
while retry < 3:
# While set to go
while run_loop:
# Try to capture a float at input time so we don't have to parse it later
try:
temp = (float(input("Enter temperature = ")))
except Exception:
# Exception catches all errors
# more info here: https://docs.python.org/3/library/exceptions.html
print("Input isn't a temperature; try again. Max 3 attempts. Attempts:",retry+1)
retry += 1
# If we can't establish a float for the first input, we'll simply skip the rest of this iteration and never set
# the run_loop flag to false, allowing loop to continue
# Print is setup so it tells me how many attempts I'm at and shows the count
break
# Ends the program if I guess too much
# Instantiate a sub loop
run_loop_sub = True
while run_loop_sub:
try:
unit = str(input("Enter C or F (for Celsius or Farhenheit) = "))
except Exception:
print("Input needs to be c/C or f/F")
# Harder to hit this since "" is a string in input, but if it fails for whatever reason
# just try again
continue
if unit.lower() == "c":
fahrenheit = (temp * 9/5) + 32
print(f'{round(fahrenheit,2)} F')
print('You know the temp now!')
# Completion condition met, set loop flag to false to exit loop after this iteration
run_loop_sub = False
elif unit.lower() == "f":
celsius = (temp - 32) * 5/9
print(f'{round(celsius,2)} C')
print('You know the temp now!')
# Completion condition met, set loop flag to false to exit loop after this iteration
run_loop_sub= False
else:
print("You need to enter either c/C or f/F")
# There is no satisfactory completion here, so don't set the close flag
# If we make it here, that means that the sub while loop was satisfied, and there is no further exceptions to
# skip this flag; we can probably end the loop
run_loop = False
#if retry == 3:
# run_loop = False
# Not sure if this helps or hurts
# After experimenting, it doesn't seem to matter if it's here
Code without comments
run_loop = True
retry = 0
while retry < 3:
while run_loop:
try:
temp = (float(input("Enter temperature = ")))
except Exception:
print("Input isn't a temperature; try again. Max 3 attempts. Attempts:",retry+1)
retry += 1
break
run_loop_sub = True
while run_loop_sub:
try:
unit = str(input("Enter C or F (for Celsius or Farhenheit) = "))
except Exception:
print("Input needs to be c/C or f/F")
continue
if unit.lower() == "c":
fahrenheit = (temp * 9/5) + 32
print(f'{round(fahrenheit,2)} F')
print('You know the temp now!')
run_loop_sub = False
elif unit.lower() == "f":
celsius = (temp - 32) * 5/9
print(f'{round(celsius,2)} C')
print('You know the temp now!')
run_loop_sub= False
else:
print("You need to enter either c/C or f/F")
run_loop = False
No Comments