Numeric Variables
a = 1, b = 2
c = a + b
>>> print(c)
3
int( variable )
[string_multiply]:
input_string = input("What's your greeting?")
count = input("How many times to repeat it?")
print( input_string * int(count) )
>>> What's your greeting? Hello >>> How many times to repeat it? >>> 3 Hello! Hello! Hello!
# In Python 3, dividing two integers gives back a float.
Exceptions
try , except
[percent_letter]: user_string = input("What's your word? ") user_num = input("What's your number? ") try: real_num = int(user_num) except: real_num = float(user_num) if not '.' in user_num: print(user_string[real_num ]) else: ratio = round( len(user_string) * real_num ) print(user_string[ratio])
Multiple Exceptions
def oh_no(arg): try: return int(arg) except ValueError: return "Invalid int!" except TypeError: return "That's not a string or a number!"
Using Else
The command that comes after else is what executes if the try was carried out without exceptions.
def oh_no(arg):
try:
num = int(input("Enter a number: "))
except ValueError:
return "That is not a number!"
else:
return num