SoFunction
Updated on 2024-11-13

Python Hands-on Exercise Cases (I)

See this and think first, how do you output him yourself?
Why does it have color?
Where to find special symbols?
Special symbols are found in the Symbol Book The Book of Symbols

/

Task 1, output book information

book_name='Java Programming Tutorial'
publish='Xi'an Electronic Science and Technology University Press'
pub_date='2019-02-02'
price=56.8
print('►→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→◀')
print('▷\t\t《',book_name,'》\t\t◁')
print('▷\t Press:',publish,'\t◁')
print('▷\t Publication date:',pub_date,'\t\t\t◁')
print('▷\t Pricing:',price,'\t\t\t\t\t◁')
print('►→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→◀')

Realize the effect:

►→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→◀
▷ "Java Programming Tutorial" ◁
▷ Publisher: Xi'an University of Electronic Science and Technology Press ◁
▷ Published: 2019-02-02 ◁
▷ Price: 56.8 ◁
►→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→◀

Task 2, output the first five of the twelve golden hairpins in Dream of the Red Chamber

1. Assignment of variables

name1='Lin Daiyu'
name2='Xue Baochai'
name3='Jia Yuanchun'
name4='Jia Tanchun'
name5='Shi Xiangyun'
print('➀\t'+name1)
print('➁\t'+name2)
print('➂\t'+name3)
print('➃\t'+name4)
print('➄\t'+name5)

Run results:

➀ Lin Daiyu
➁➁ Baochai Xue
➂ Jia Yuanchun
➃ Jia Tanchun
➄ Shi Xiangyun

2. The way of the list

lst_name=['Lin Daiyu','Xue Baochai','Jia Yuanchun','Jia Tanchun','Shi Xiangyun']
lst_sig=['➀','➁','➂','➃','➄']
for i in range(5):
    print(lst_sig[i],lst_name[i])

If there is no [i] it will just output the array:

➀ Lin Daiyu
➁➁ Baochai Xue
➂ Jia Yuanchun
➃ Jia Tanchun
➄ Shi Xiangyun

3. The dictionary approach

d={'➀':'Lin Daiyu','➁':'Xue Baochai','➂':'Jia Yuanchun','➃':'Jia Tanchun','➄':'Shi Xiangyun'}
print('''------------------------------------------------------''')
for key in d:
    print(key,d[key])
    print('zip-----------------------------------')
for s,name in zip(lst_sig,lst_name):
    print(s,name)

Run results:

➀ Lin Daiyu
zip-----------------------------------
➁➁ Baochai Xue
zip-----------------------------------
➂ Jia Yuanchun
zip-----------------------------------
➃ Jia Tanchun
zip-----------------------------------
➄ Shi Xiangyun
zip-----------------------------------
➀ Lin Daiyu
➁➁ Baochai Xue
➂ Jia Yuanchun
➃ Jia Tanchun
➄ Shi Xiangyun

Task 3, output of books and audio-visual medals

There are colors:

print('\033[0;35m\t\t\t Books Audio-visual Medal\033[m')
print('\033[0;35m---------------------------------------------\033[m')
print('\033[0;32m❀Book and Audio-visual Medallion\t\t\tBreathless Exclusive Event\033[m')
print('\033[0;34m❤Exclusive Offers\t\t\☏\t\t\offer alerts\033[m')
print('\033[0;35m---------------------------------------------\033[m')

Task 4. Export your body metrics

BMI based on height and weight

height=170
weight=50.5
bmi=weight/(height+weight)
print('Your height is:',height)
#print('Your weight is:'+str(weight))
print('Your weight is:',weight)
print('The index of your BMI is:',bmi)

Your height is: 170
Your weight is: 50.5
Your BMI is: 0.2290249433106576

Formatting for decimals:

  • print('The index of your BMI is:' + '{:0.2f}'.format(bmi))
  • Your BMI is 0.23.

To this point this article on python hands-on practice cases are introduced to this article, more related python practice content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!