Python Programming Exercises with Solutions, Exams of Nursing

A collection of Python programming exercises designed for practice and learning. Each exercise focuses on a specific programming concept, such as data types, file handling, and mathematical operations. Clear instructions and sample code solutions are provided for each exercise, making it a valuable resource for students learning Python.

Typology: Exams

2023/2024

Available from 12/26/2024

solutionguides
solutionguides 🇺🇸

916 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
h;
/
12
WGU D335 Practice Test 2 With Questions And 100% SURE
ANSWERS
1. Create h;a h;solution h;that h;accepts h;three h;integer h;inputs h;representing h;the h;number h;of
h;times h;an h;employee h;travels h;to h;a h;job h;site. h;Output h;the h;total h;distance h;traveled h;to
h;two h;decimal h;places h;given h;the h;following h;miles h;per h;employee h;commute h;to h;the h;job
h;site. h;Output h;the h;total h;distance h;traveled h;to h;two h;decimal h;places h;given h;the
h;following h;miles h;per h;employee h;commute h;to h;the h;job h;site:
Employee h;A: h;15.62 h;miles
h;Employee h;B: h;41.85 h;miles
Employee h;C: h;32.67 h;miles: h;times_traveledA h;= h;int(input())
h;times_traveledB h;= h;int(input())
times_traveledC h;= h;int(input())
h;employeeA h;= h;15.62 h;#miles
h;employeeB h;= h;41.85 h;#miles
h;employeeC h;= h;32.67 h;#miles
distance_traveledA h;= h;times_traveledA h;* h;employeeA
h;distance_traveledB h;= h;times_traveledB h;* h;employeeB
h;distance_traveledC h;= h;times_traveledC h;* h;employeeC
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python Programming Exercises with Solutions and more Exams Nursing in PDF only on Docsity!

1 h;/

WGU D335 Practice Test 2 With Questions And 100% SURE

ANSWERS

1. Createh;ah;solutionh;thath;acceptsh;threeh;integerh;inputsh;representingh;theh;number h;of

h;times h;an h;employee h;travels h;to h;a h;job h;site. h;Output h;the h;total h;distance h;traveled h;to h;twoh;decimalh;placesh;givenh;theh;followingh;milesh;perh;employeeh;commuteh;toh;the h;job h;site. h;Output h;the h;total h;distance h;traveled h;to h;two h;decimal h;places h;given h;the h;following h;miles h;per h;employee h;commute h;to h;the h;job h;site: Employeeh;A:h;15.62h;miles h;Employee h;B:h;41.85 h;miles Employeeh;C:h;32.67h;miles:h;times_traveledAh;=h;int(input()) h;times_traveledB h;= h;int(input()) times_traveledCh;=h;int(input()) h;employeeA h;= h;15.62 h;#miles h;employeeB h;= h;41.85 h;#miles h;employeeC h;= h;32.67 h;#miles distance_traveledA h;= h;times_traveledA h;* h;employeeA h;distance_traveledB h;= h;times_traveledB h;* h;employeeB h;distance_traveledCh;=h;times_traveledCh;*h;employeeC

2 h;/

total_miles_traveled h;= h;distance_traveledA h;+ h;distance_traveledB h;+ h;distance_trav- h;eledC print('Distance:h;{:.2f} h;miles'.format(total_miles_traveled))

  1. Createh;ah;solutionh;thath;acceptsh;anh;inputh;identifyingh;theh;nameh;ofh;ah;texth;file,h;for h;example,h;"WordTextFile1.txt".h;Eachh;texth;fileh;containsh;threeh;rowsh;withh;oneh;word h;per h;row. h;Using h;the h;open() h;function h;and h;write() h;and h;read() h;methods, h;interact h;with h;the h;input h;text h;file h;to h;write h;a h;new h;sentence h;string h;composed h;of h;the h;three h;existing h;words h;to h;the h;end h;of h;the h;file h;contents h;on h;a h;new h;line. h;Output h;the h;new h;file h;contents.: h;file_name h;= h;input() with h;open(file_name, h;'r') h;as h;f: word1 h;= h;str(f.readline()).strip() h;word2 h;= h;str(f.readline()).strip() h;word3 h;= h;str(f.readline()).strip() f h;= h;open(file_name, h;'r') h;linesh;=h;f.read().splitlines() h;lines h;= h;' h;'.join(lines) h;f.close() print(f'{word1}\n{word2}\n{word3}\n{lines}')
  2. Create h;a h;solution h;thath;accepts h;anh;integer h;input h;representing h;anyh;number h;of h;ounces. h;Output h;the h;converted h;total h;number h;of h;tons, h;pounds, h;and h;remaining h;ouncesh;basedh;onh;theh;inputh;ouncesh;value.h;Thereh;areh; 16 h;ouncesh;inh;ah;poundh;and h;2,000 h;pounds h;in h;a h;ton.: h;ounces_per_pound h;= h; 16

4 h;/

various_data_typesh;=h;[516,h;112.49,h;True,h;"meow",h;("Western",h;"Governors", h;"University"), h;{"apple": h;1, h;"pear": h;5}] Using h;the h;built-in h;function h;type() h;and h;getting h;its h;name h;by h;using h;the h;.name h;attribute,h;output h;data h;type h;(e.g.,h;int",h;"float",h;"bool",h;"str") h;based h;on h;the h;input h;index h;value h;of h;the h;list h;element.: h;index_value h;= h;int(input()) name h;= h;various_data_types[index_value] h;data_type h;= h;type(name). name h;print(f"Elementh;{index_value}:h;{data_type}")

  1. Create h;a h;solution h;that h;accepts h;an h;integer h;input. h;Import h;the h;built-in h;module h;math h;and h;use h;its h;factorial() h;method h;to h;calculate h;the h;factorial h;of h;the h;integer h;input.h;Output h;the h;value h;of h;the h;factorial,h;as h;well h;as h;a h;Boolean h;value h;identifying h;whether h;the h;factorial h;output h;is h;greater h;than h;100.: h;import h;math fact h;= h;int(input()) x h;= h;math.factorial(fact)

5 h;/

print(x) if h;x h;> h;100: print('True') h;else: print('False')

  1. Create h;a h;solution h;that h;accepts h;any h;three h;integer h;inputs h;representing h;the h;base h;(b1,h;b2) h;and h;height h;(h) h;measurements h;of h;a h;trapezoid h;in h;meters.h;Output h;the h;exact h;area h;of h;the h;trapezoid h;in h;square h;meters h;as h;a h;float h;value.h;The h;exact h;area h;of h;a h;trapezoid h;can h;be h;calculated h;by h;finding h;the h;average h;of h;the h;two h;base h;measurements, h;then h;multiplying h;by h;the h;height h;measurement. Trapezoid h;Area h;Formula:Ah;= h;[(b1 h;+ h;b2) h;/ h;2] h;* h;h:h;b1 h;= h;int(input()) h;b2 h;= h;int(input()) h h;= h;int(input()) area_value h;= h;((b1 h;+ h;b2) h;/2) h;* h;h print('Trapezoidh;area:h;{:.1f}h;squareh;meters'.format(area_value))
  2. Createh;ah;solutionh;thath;acceptsh;anh;integerh;inputh;representingh;theh;ageh;ofh;ah;pig. h;Import h;the h;existing h;module h;pigAge h;and h;use h;its h;pre-built h;pigAge_converter() h;function h;to h;calculate h;the h;human h;equivalent h;age h;of h;a h;pig. h;A h;year h;in h;a h;pig's h;life h;is h;equivalent h;toh;five h;years h;inh;a h;human's h;life.h;Outputh;theh;human-equivalenth;age h;of h;the h;pig.: h;import h;pigAge def h;pigAge_converter(pig_age): return h;pig_age h;* h; 5 h;pig_ageh;=h;int(input()) converted_pig_age h;= h;pigAge_converter(pig_age) h;print(f'{pig_age}h;ish;{converted_pig_age} h;inh;humanh;years')

7 h;/

third_output h;= h;str(num1) h;+ h;str(num2) h;+ h;str(num3) h;+ h;str(num4) h;+ h;str(num5) h;print('Integer: h;{}'.format(first_output)) print('Float:h;{}'.format(second_output)) h;print('String: h;{}'.format(third_output))

  1. Create h;a h;solution h;that h;accepts h;a h;string h;input h;representing h;a h;grocery h;store h;item h;and h;an h;integer h;input h;identifying h;the h;number h;of h;items h;purchased h;on h;a h;recenth;visit.h;Theh;followingh;dictionaryh;purchaseh;listsh;availableh;itemsh;ash;theh;key h;with h;the h;cost h;per h;item h;as h;the h;value. purchase h;= h;{"bananas":h;1.85,h;"steak":h;19.99,h;"cookies":h;4.52,h;"celery":h;2.81, "milk":h;4.34} h;Additionally, If h;fewer h;than h;ten h;items h;are h;purchased,h;the h;price h;is h;the h;full h;cost h;per h;item. If h;between h;ten h;and h;twentyh;items h;(inclusive) h;are h;purchased,h;the h;purchase h;gets h;a h;5% h;discount. Ifh;twenty-oneh;orh;moreh;itemsh;areh;purchased,h;theh;purchaseh;getsh;ah;10%h;discount. h;Outputh;theh;chosenh;itemh;andh;totalh;costh;ofh;theh;purchaseh;toh;twoh;decimalh;places.- : h;store_item h;= h;input() h;num_itemsh;=h;int(input()) total_costh;=h;purchase[store_item]h;h;num_items h;if h;num_items h;< h;10: print(store_item,h;"${:.2f}".format(total_cost)) h;if h;num_items h;in h;range(10,21): discount h;= h;total_cost h; h;0.05 h;total_cost h;=h;total_costh;-h;discount print(store_item,h;'${:.2f}'.format(total_cost))

8 h;/

h;if h;num_items h;>= h;21: discount h;= h;total_cost h;* h;0.10 h;total_cost h;=h;total_costh;-h;discount print(store_item, h;'${:.2f}'.format(total_cost))

  1. Create h;a h;solution h;that h;accepts h;an h;integer h;input h;representing h;a h;9-digit h;unformattedh;student h;identificationh;number.h;Outputh;theh;identificationh;number h;as h;a h;string h;with h;no h;spaces.: h;student_id h;= h;int(input()) student_id_stringh;=h;str(student_id) h;first3 h;= h;student_id_string[0:3] h;second2 h;= h;student_id_string[3:5] h;third4 h;= h;student_id_string[5:] h;print(f'{first3}-{second2}-{third4}')
  2. Create h;a h;solution h;that h;accepts h;an h;integer h;input h;identifying h;how h;many h;sharesh;ofh;stockh;areh;toh;be h;purchasedh;fromh;theh;Oldh;Townh;Stock h;Exchange,

10 h;/

h;ature h;in h;degrees h;Fahrenheit. h;Output h;a h;description h;of h;the h;water h;state h;based h;on h;the h;following h;scale: If h;the h;temperature h;is h;below h;33° h;F,h;the h;water h;is h;"Frozen". If h;the h;water h;is h;between h;33° h;F h;and h;80° h;F h;(including h;33),h;the h;water h;is h;"Cold". If h;the h;water h;is h;between h;80° h;F h;and h;115° h;F h;(including h;80), h;the h;water h;is h;"Warm". h;If h;the h;water h;is h;between h;115° h;F h;and h;211° h;(including h;115) h;F, h;the h;water h;is h;"Hot". h;If h;the h;water h;is h;greater h;than h;or h;equal h;to h;212° h;F, h;the h;water h;is h;"Boiling". Additionally,h;outputh;ah;safetyh;commenth;onlyh;duringh;theh;followingh;circum- h;stances: If h;the h;water h;is h;exactly h;212° h;F,h;the h;safety h;comment h;is h;"Caution:h;Hot!" Ifh;the h;water h;temperature h;is h;less h;thanh;33°h;F,h;the h;safetyh;comment h;is h;"Watchh;out h;for h;ice!": h;temperature h;= h;int(input()) if h;temperature h;<33:

11 h;/

print('Frozen') h;print('Watchh;outh;forh;ice!') if h;temperature h;in h;range(33, h;80): print('Cold') if h;temperature h;in h;range(80, h;115): print('Warm') if h;temperature h;in h;range(115, h;211): print('Hot') if h;temperature h;>= h;212: print('Boiling') if h;temperature h;== h;212: print('Caution:h;Hot!')

  1. Create h;a h;solution h;that h;accepts h;one h;integer h;input h;representing h;the h;index h;value h;for h;any h;of h;the h;string h;elements h;in h;the h;following h;list: frameworksh;=h;["Django",h;"Flask",h;"CherryPy",h;"Bottle",h;"Web2Py",h;"Turbo- h;Gears"] Output h;the h;string h;element h;of h;the h;index h;value h;entered.h;The h;solution h;should h;be h;placed h;in h;a h;try h;block h;and h;implement h;an h;exception h;of h;"Error" h;if h;an h;incom h;mypatible h;integer h;input h;is h;provided: h;index_value h;= h;int(input()) try: if h;index_value h;== h;0: h;print(frameworks[0] ) h;if h;index_value h;== h;1: h;print(frameworks[1]