So far, we've looked at instances where we need to get integer type input. This has been really easy because we've been using
mynumber = int(input("Enter a Number: ")
This EXPECTS us to put an integer in, and when we don't it will throw an exception!
Hold up, DuckTales fans—we’re taking a detour to tackle a sneaky Python quirk! Flintheart Glomgold, Scrooge McDuck’s stingiest rival, is back with wild plots to get rich, but his henchmen keep handing him strings—like '100' or '5.25'—via input().
Strings won’t cut it for counting cash or building traps—he needs numbers! In this bonus lesson, you’ll master turning those stringy inputs into integers, floats, and more with int(), float(), and a few tricks. Let’s power up Glomgold’s greedy gains and see why this matters!
Glomgold’s all about numbers—gold bars, cash piles, wins over Scrooge—but input() only gives strings. Converting them is like turning Glomgold’s brags into real loot! Without this, he can’t add, multiply, or compare his haul—his schemes would crash harder than his fake submarines.
When we get input as strings, we can use STRING METHODS to check and see what type of string we have!!
Nothing special needed. This is almost the same as int(input()) but you do it in 2 steps
Nothing special needed. This is almost the same as float(input()) but you do it in 2 steps
This is useful for money or decimal operations
Note the special print to show only 2 decimal places
Returns True if all characters in the string are digit characters (0-9, and some Unicode digits like superscripts).
Once we know that, convert to an int
Returns True if all characters are strictly decimal digits—only the basic 0-9 characters (no Unicode extras)
Once we know that, convert to an int
Make sure the string is all letters
NO NUMBERS ALLOWED
You'll notice that none of these string methods can really help us to validate a float...
If we type number like 3.14, we see:
.isdigit() - FAILS
.isnumeric() - FAILS
isdecimal() - FAILS
etc...
We need to use either a custom function, or just be super slick with a try-except...
This does the following:
Takes in a string
splits it into a list based on the '.' character
checks both parts of the splits to see if it's a number
Just try to convert it to a float in a try / except
If it doesn't throw an error, you win
Flintheart Glomgold’s launching a scheme to loot Scrooge’s Money Bin with a giant catapult! He needs his henchmen to input the loot count, catapult range, and a secret code—then he’ll fire away. Help him convert strings to numbers and check inputs with functions, so his plan doesn’t flop!
Requirements:
Use functions for each sub-task.
Use int(), float(), .isdigit(), .isalpha(), .isalnum().
Include simple conversions and type checks.
Convert Loot Count (Simple Conversion)
Function: get_loot_count()
Ask for gold coins (integer) with input().
Use int() to convert—print the count.
Sample: '50' → 50 coins.
Convert Catapult Range (Simple Conversion)
Function: get_range()
Ask for range in meters (float) with input().
Use float()—print the range (2 decimals).
Sample: '75.5' → 75.50 meters.
Check Secret Code (Type Check)
Function: check_code()
Ask for a launch code (e.g., 'FIRE42').
Use .isalnum()—print if it’s valid (letters and digits only).
Sample: 'FIRE42' → Valid code!, 'FIRE 42' → Invalid code!.
Launch Report (Combine)
Function: launch_report()
Call the above functions.
Use .isdigit() on loot count—if valid, calculate loot value (count × 10).
Print loot count, range, code status, and value.
Sample Output
Glomgold’s teamed up with Magica De Spell to steal Scrooge’s Number One Dime, but their rivalry’s heating up! They’re splitting loot, setting traps, and naming their evil pact—using numeric inputs and string checks. Build functions to convert inputs, validate them with picky rules, and settle their score—Glomgold’s not sharing easily!
Requirements:
Use functions for each sub-task.
Use int(), float(), .isdigit(), .isnumeric(), .isdecimal(), .isalpha(), .isalnum().
Mix conversions and complex type checks.
Convert Loot Split (Simple Conversion)
Function: split_loot()
Ask for total cash (float) with input().
Use float()—split it (cash ÷ 2), print each share (2 decimals).
Sample: '99.99' → Glomgold: $50.00, Magica: $49.99.
Convert Trap Count (Simple Conversion)
Function: set_traps()
Ask for traps (integer) with input().
Use int()—print the count.
Sample: '10' → 10 traps.
Check Pact Name (Type Check)
Function: name_pact()
Ask for a pact name (e.g., 'Doom').
Use .isalpha()—must be letters only, print if valid.
Sample: 'Doom' → Valid name!, 'Doom42' → Letters only!.
Validate Trap Code (Complex Check)
Function: trap_code()
Ask for a trap code (e.g., '123').
Use .isdecimal() (stricter than .isdigit())—must be digits, print if valid.
Use .isnumeric()—compare, note if it’s broader (rare case).
Sample: '123' → Valid decimal code! Numeric too!.
Score Showdown (Combine)
Function: showdown()
Call above functions.
Use .isdigit() on traps—if valid, calculate trap cost (traps × 5.5).
If pact name is valid and cash > trap cost, Glomgold wins—print results.
Add a function secret_code() to validate a code:
Must be exactly 7 characters: 4 letters (alphabetic) + 3 digits.
Example: 'EVIL123' (valid), 'BAD12' (invalid), 'EVIL12A' (invalid).