forked from CalebCurry/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-user-input.py
More file actions
49 lines (34 loc) · 1.59 KB
/
04-user-input.py
File metadata and controls
49 lines (34 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#Here is a simple exmaple on how to get user input
#we've talk about output with print, but this is just half of input + output
#it's besst practice to tell the user what to do:
print("What is your name?")
name = input()
#Now where does the value they type go?
#the input method actually returns it, so save it to a variable.
#we can then use it like any other variable (because it is just a variabe now)
print("hello " + name)
#althougn this seems silly or simple, this is actually quite revolutionary.
#This is the first point in time where we can make our applications dynamic
#dynamic == changing. The output changes based on the input! WOW!
#input always reads as a string. Example:
print("What is your favorite number?")
num1 = input()
print("what is your second favorite mumber?")
num2 = input()
print(num1 + " + " + num2 + " = " + num1 + num2) # 7 + 5 = 75??? WHAT!?
########## TYPES AND CASTING ##########
#At this point we know there are different types
#but how do we check what type and modify it?
print(type(num1)) #num1 is of type str (string, that is)
#We can cast it to a new type like so:
new_num1 = int(num1)
new_num2 = int(num2)
print(type(new_num1), type(new_num2))
print(num1 + " + " + num2 + " = " + str(new_num1 + new_num2)) #Better
#if you know you're going to be getting a number, it's best to cast it right away.
#Lets try it again, but this time let's showcase another type...float!
#float allows values after a decimal point, such as 10.5.
print("Give me a 3rd number (with a decimal this time):")
num3 = float(input())
print("all the numbers added:")
print(num3 + new_num1 + new_num2)