Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
0 As you know from the previous lectures we have defined this function. 1
And here we are calling it. 2
So it checks if the given temperature is greater than seven and it returns a warm 3
if that is true, and cold if the temperature is equal to seven or less than seven that's fine. 4
But what is the use of this function in a real life setting? Like who is giving this input? 5
You as a programmer you are writing this function for someone else so that they can use it but you don't 6
expect them to edit the Python file like to have an editor like Visual Studio Code or other editors, 7
and then you cannot just tell them to go to the line 7 and change the values and then execute that code 8
to get different outputs depending on the value. 9
So how about prompting the user to enter a value, and then the user types in the value and then we get that 10
value and process it in our function. 11
In that case we get that value and put it in here in the function call. 12
Let's do that. 13
So we've got a function there. We want to get a value from the user. To get a value from the user 14
use the input function, the input function gets an argument and that is the message you want to show 15
to the user on the command line. 16
For example enter "Enter temperature". 17
maybe put a colon there if you like, you can write whatever you like. 18
Let me save this script and execute it. 19
You see now I executed it and this input function what it does is it freezes the execution of a program 20
and waits for user input on the command line. 21
So if I put inputs something here like seven in this case what is going to happen is that this expression 22
here will be equal to seven. 23
However because we didn't do anything here nothing will happen. If we print out this value 24
for example 7 you'll get printed out the value of this which is 7. 25
So there you can put anything you want. 26
Hi there. 27
And you get "Hi there" printed out. What I would suggest is instead of printing this out you'd first 28
want to put it in a variable, that would make it more readable so user input, and than you can do whatever 29
you want with that variable like lower it, so to print out the lowercase version of that. 30
Hi hi. So you get hi hi with lowercase letters, okay. 31
Back to our function. 32
Let me now try to print out the weather condition function output with a value of, well user inputs, whatever 33
the user enters there as inputs, so I'll execute the program now. 34
The program will ask me with enter temperature. 35
The program will ask me to enter a temperature so the execution is frozen at this point. 36
This line has not been executed yet. 37
It will be executed after we give a value here and press enter. 38
So let me write down six. 39
Let's see what we are going to get 40
this time, we get an error. 41
Let's read the error carefully. 42
It's about file basics.py which is our file, okay. 43
The error happened at first 44
at this line here. 45
So you print weather condition user input. When Python tried to execute this line it couldn't do it and 46
why? 47
Well you have to follow the error. 48
So still line basics.py line 2, at this line. 49
So when Python tried to execute this line, that line called some other things in the script and the 50
error was in this line. 51
Line 2. 52
So when the function call tried to call the function, it tried to execute this line. 53
So if temperature... It tried to compare 5 with 7, and then it got this error when it's tried to compare 54
5 with 7. 55
So when it's tried to execute this line in other words. See what it says. 56
Type error. 57
This operator is not supported between instances of string and integer, so string and int. 58
Which means this was a string and this was an int. 59
So our user input was 7, was passed in here and that was compared with 7. 60
It seems that our input the user input was a string and that is true. 61
That was the point I was trying to make here because whatever input the user writes in here even if 62
it's a number. 63
Python will actually converted to a string. 64
So 7 in fact will be converted to a string like with quotes or double quotes inside Python but that 65
has an easy fix. 66
What you can do is you can convert this value which is the string 7, 67
you can convert it into a float. 68
Be careful with the parentheses, so float is a function. 69
It has an open parentheses in here and it has a closing parentheses in here. 70
Input is also a function with its own opening and closing parentheses, so no if I do that, enter temperature 5, 71
I'm going to get the correct answer and let me be transparent with you. 72
Let's again input gets user input "Enter some input". 73
Let me now 74
print out that type of user input, execute, enter 6, you'll see that 6 actually is a string. 75
That's why I am converting it into a float, so that 6 this time will be a float actually which looks like 76
this. 77
You can print out the actual number here, the actual convert number, the float version of the number. 78
Execute again. 79
6, so 6.0. 80
You can also use int if you like. 81
6 so you'll get six which is equal to six point zero of course, but floats would be a better choice because 82
I can show you why, if you enter 6.3 for example int will not be able to convert it because 83
that is like doing int 6.3 in double quotes so int will get confused you will not know how 84
to convert that into an integer. 85
It does however convert 6 86
if it was a simple six without a decimal point. Float will convert both. 87
Sorry both 6 and 6.3. 88
And that's how you use user input in Python.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.