Ex5 - Breaking the code

name = 'Zed A. Shaw'
age = 35 # not a lie
height = 74 # inches
weight = 180 # lbs
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'

print(f"Let's talk about {name}.")
print(f"He's {height} inches tall.")
print(f"He's {weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {eyes} eyes and {hair} hair.")
print(f"His teeth are usually {teeth} depending on the coffee.")

# this line is tricky, try to get it exactly right
total = age + height + weight 
print(f"If I add {age}, {height}, and {weight} I get {total}.")
print(f"Let's talk about {print(name)}.")
Zed A. Shaw # It just prints the variable and ignores the rest of the string

print(f"Let's talk about {input()}.") # this works  and let's you get user input 

print(f"He's got {eyes, {} } eyes and {hair} hair.".format(eyes)) # this worked and gave 
He's got ('Blue', Blue) eyes and Brown hair.

print(f"He's {height:.3} inches tall.") # gives an error 
ValueError: Precision not allowed in integer format specifier

which lead me to :.3f it let’s your round a float to however many significant figures.

im not sure whats all going on here. I dont know much about string interpolation, but I dont think its doable with a fucntion. Also using .format() on a f string?
Half of your comments are not commented out.
You do lose the ValueError when you use {height:.3f} .
um…but what are you asking? Or was that showing format code, which I hadnt seen before this.

1 Like

I was just trying to show what I got from trying to break the code I thought it was interesting. Yes showing format code {height:.3f}.

1 Like

A lot of those format codes come from the C printf function:

https://www.cplusplus.com/reference/cstdio/printf/

They might not be exactly the same but you might find some secret knowledge with that reference.

1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.