Comments by "swallowedinthesea11" (@swallowedinthesea11) on "do you need to be good at MATH to learn Python? // Python RIGHT NOW!! // EP 3" video.
-
4
-
3
-
3
-
3
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
@BadRay27 Nice!
A dictionary is a useful way to pair whatever to whatever. You can pair phone numbers to names, pair ratings to movies, pair GPAs to students, pair prices to drinks, etc.
models = {
'Belle Delphine': 5,
'Kim Kardashian': 4,
'Anita Sarkeesian': 5
}
print('Belle Delphine, Kim Kardashian, or Anita Sarkeesian')
choice = input('Who is the prettiest? ')
print( 'You chose', choice, 'who is rated a', models[choice] )
models is a variable that has the dictionary of those three girls.
The string 'Belle Delphine' is a key with the value of integer 5.
The string 'Kim Kardashian' is a key with the value of integer 4.
The string 'Anita Sarkeesian' is a key with the value of integer 5.
Let's type in 'Belle Delphine' (without the quotations).
You get 'You chose Belle Delphine who is rated a 5'
Where did 5 come from?
models[choice] equates to models['Belle Delphine'] which equates to 5 because 'Belle Delphine' has the value of 5.
So, let's try the order:
drinks = { 'Coke': 100, 'Pepsi': 99, 'Sprite': 5 }
order = input('What would you like? ')
quantity = input('How many? ')
total = drinks[order] * int(quantity) # use int() to convert the string into an integer
print('You ordered', quantity, order, 'and the total is', total)
2
-
2
-
2
-
2
-
2
-
2
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
Friendly tip: we now use the new f-strings which was introduced in version 3.8. Concatenation is pretty archaic, confusing to read, requires extra work, and so 1990s.
That said, your error is in the following:
You have to remember that the input function always returns a string data type. Hence why you got 11111111 because typing 1 is not an integer data type, it's a string data type '1' Notice the quotes; they signify that '1' is a string data type.
print ("your " + order + " will cost " + (8 * Quantity) + "$ \n ")
Because you're concatenating, you need to convert Quantity into an integer data type by using the integer function:
print ("your " + order + " will cost " + ( 8 * int(Quantity) ) + "$ \n ")
Now run it. Did you get an error?
You have to go the extra step by converting the value into a string data type because concatenating only works for string data types by using the string function:
print ("your " + order + " will cost " + str( ( 8 * int(Quantity) ) ) + "$ \n ")
Now everything works.
Using f-strings, it would look like this:
print ( f"your {order} will cost { 8 * int(Quantity) } $ \n ")
Read the following to learn more about f-strings:
There is a rule when you want to concatenate a string data type with an integer data type: you can't concatenate different data types - the data type must be a string. You can use type coercion to convert an integer into a string using str():
characterAge = 500
print( 'Belle Delphine is ' + str(characterAge) ) # using str() to convert the integer
print( 'Belle Delphine is', characterAge )
print( f'Belle Delphine is {characterAge}' )
# Above, f-strings are the newest way since version 3.8 VS version 3.6 that Mike used
# As you can see, concatenation is archaic as f-strings are preferred. Just imagine how ugly having
# a bunch of + symbols would be!
Explanation:
age = 46
print('Batman is aged: ' + age)
^Why do we get an error? Because Python hates concatenating different data types. In order to concatenate, all the data types must be a string data type! 46 is an integer data type. To change the type, we can use str(variable here):
age = 26
print( 'Batman is aged: ' + str(age) )
print( f'Batman is aged {age}' )
age = input('How old are you? ') # let's input a number 100
print('Frank Castle is aged: ' + age)
^Why do we not get an error? Because Python loves concatenating the same data type which is the string data type. '100' is a string data type because it has the quotes. 100 looks like a number (just ask anyone you see) but whenever you use the input() function to get user input, anything the user types in automatically becomes a string data type.
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1