Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet43/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   39   40   41   42   43   44   45   46   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Current Type

Data Value

Converting to

Proper Code

Output

Integer

9

string

str(9)

'9'

Integer

5

Float

float(5)

5.0

Float

5.6

Integer

int(5.6)

5

string

‘9’

Integer

int('9')

9

string

‘True’

Boolean

bool('True')

True

Boolean

True

Integer

int(True)

1

As you can see, there are several ways to type convert; you just need to use the keyword for each defining data type. The boolean type of True converts to an integer of 1 because the True and False values represent 1 and 0, respectively. Also, converting a float to an integer will just truncate the decimal, as well as any numbers to the right of the decimal.
Note not all data types can be converted properly. There are limits.

Converting User Input


Let’s try working with a user’s input in order to add 100 to whatever they type:

# working with user input to perform calculations ans = input("Type a number to add: ") print( type(ans) ) # default type is string, must convert result = 100 + int(ans) print( "100 + { } = { }".format(ans, result) )

Go ahead and run that cell. Inputting the number “9” will give us a proper result; however, this conversion would not work well with the word “nine” because the default return type for input is a string as noted by the first print statement in this cell.

Handling Errors


In the last cell, we convert the user input to an integer; however, what if they put in a word instead? The program would break right away. As a developer, we must assume that the user won’t put the proper information that we expect them to. To handle this issue, we’re going to introduce try and except blocks. Try and except are used to catch errors. It works by trying to run what is inside the try block; if it doesn’t produce an error, then it continues without hitting the except block; however, if an error occurs, then the code in the except block runs. This is to make sure your program doesn’t stop running if an error pops up. This is a generic way to handle errors; there are many other methods like using the functions isalpha() and isalnum(). Let’s look at an example using the try and except blocks:

# using the try and except blocks, use tab to indent where necessary try:
ans = float( input("Type a number to add: ") ) print( "100 + { } = { }".format(ans, 100 + ans) ) except:
print("You did not put in a valid number!")
# without try/except print statement would not get hit if error occurs print("The program did not break!")

Go ahead and run that cell. Try inputting different answers including non-numbers. You’ll notice that our nonvalid print statement will output if you don’t input a number. If we didn’t have the try and except in place, the program would break, and the last print statement wouldn’t occur.
ChApTer 3 User InpUT And CondITIonAls

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   39   40   41   42   43   44   45   46   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish