''' Purpose: demonstrate int and float numeric input processing ''' # get inputs x_reply = input( 'Enter base (integer): ' ) n_reply = input( 'Enter exponent (decimal): ' ) # We want to perform the operation x ** n (exponentiation) # We prompt the user for two values up above using input. # REMEMBER input() is a function that hands back whatever the user # entered as a string. # Sooo what that means is if I write 5 in the console after the first # prompt, x_reply stores the value of that 5 as a string! # x_reply = "5" # Likewise same thing for n_reply stores whatever use enters into n_reply # answer = x_reply ** n_reply # Why'd it blow up? Because they're strings!! x_reply and n_reply # ARE STRINGS!!! "2" and "2.5" # SOOO WE CAST IT. Cast it into a number form using functions int() and # float(). print() # This just prints a blank line. # It doesn't affect input prompts.. more for separation. # convert inputs to numerics x = int( x_reply ) # cast string x_reply to get an int n = float( n_reply ) # cast string n_reply to get a decimal # So again we did typecasting (casting) which converts different types # into other types. We used int( x_reply ) to convert the string of x_reply # "2" into number 2 stored in x :) This is something you should keep track of! # A string of a number can be converted into a number! # compute x to the nth power value = x ** n # You just need two numbers for the exponentiation operator :) # Can be ints or floats :) You can't do exponentiation # with two strings even if the strings are like "3" or "3.5" # You have to convert them into numbers first. # display result print( value ) # If I wanted a number and I say reply = input("Enter a number:") # and the user entered "two" the string "two" is stored in reply # and int(reply) would be like "that's not a number..." and it # wouldn't work. It has to be "2" or "3" or a number inside the string\ # Play around with the numbers and casting! # x = 3 # y = float(x) # print(y) will print out 3.0 # x = 3.25 # y = int(x) # print(y) will print out 3 # This is the difference between castings! int("3") will return 3 # float("3.25") will return 3.25 NOT AS STRINGS :D # Okay so here's the thing.. when you enter something # in response to a prompt in the console whatever you type # is automatically handed back as a string # so it's not like you have to put "3" or quotations around it # to make it a string. reply stores the user input AS a string. # reply now HOLDS a string. # You might have to convert back into a string if we ask you to. # More on this later promise!