最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

加拿大瑞爾森大學(xué) Python習(xí)題 Lab 1

2023-08-05 08:53 作者:逃跑的叮當(dāng)貓  | 我要投稿

前段時(shí)間教家里的少爺學(xué)python,做了他們大學(xué)的習(xí)題,基本上做完這8章習(xí)題也就學(xué)會(huì)python了,現(xiàn)在分享給大家,有興趣的同學(xué)自取。測(cè)試采用Spyder IDE?

Python 3.8.10 64-bit | Qt 5.15.2 | PyQt5 5.15.7 | Windows 10?


Lab 1 – CPS 106?

This lab gives you more practice using basic elements of python. The following are based on exercises from the book: Introduction to Computation and Programming Using Python, by John Guttag.?

1. Write a program that examines three variables x , y , and z, and prints the largest odd number among them. If none of them are odd, it should print a message to that effect.?

Test you program on the inputs:?

x = 234, y = 653, z = 34,?

x = 56, y = 776, z = 787,?

x = 79, y = 87465, z = 764563?

Hint: this is done using nested if/else statements.?

2. Write a program that asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect. This is a more general version of Question 1. For example of the integers were: 10, 9, 7, 12, 2, 5, 15, 100, 90, 60, then the program would print 15 as the largest odd number.?

Hint: use a while or for loop to ask the user for a number in each iteration.?

3. Write a program that adds the amounts of money in a list. An example input is: 23 CAD, 345 CAD, 55 CAD. The output for this input is 423 CAD.?

Test you program on the inputs:?

527 CAD, 392 CAD, 477 CAD, 495 CAD, 569 CAD, 380 CAD, 38 CAD, 19 CAD, 473 CAD, 31 CAD 478 CAD, 538 CAD, 614 CAD, 987 CAD, 632 CAD, 525 CAD, 955 CAD, 322 CAD, 357 CAD, 526 CAD?

Hint: you can use the split function with the separator ‘ ‘ on the input as was done in the class. The resulting list will also contain non-numeric values like “CAD,”. The string object has a function called isnumeric() that checks if a string is a number. For example, if s = “123” then s.isnumeric() return True, but if s = “yuew” the s.isnumeric() return False.

答案

習(xí)題1-2 find largest odd

# -*- coding: utf-8 -*-

"""

Created on Fri Jan? 6 18:07:18 2023


@author: pc

"""


? ??

def find_largest_odd(numbers):

? ? largest_odd = None

??

? ? for number in numbers:

? ? ? ? if number % 2 == 1:

? ? ? ? ? ? if largest_odd is None or number > largest_odd:

? ? ? ? ? ? ? ? largest_odd = number


? ? if largest_odd is not None:

? ? ? ? print(largest_odd)

? ? else:

? ? ? ?print("There are no odd numbers.")


numbers1 = [10, 9, 7, 12, 2, 5, 15, 100, 90, 60]

find_largest_odd(numbers1)


習(xí)題 1-3-1 amounts

# -*- coding: utf-8 -*-

"""

Created on Sat Jan? 7 10:00:30 2023


@author: pc

"""


def find_largest(numbers):

? largest = 0

??

? for number in numbers:

? ? ? largest += int(number.split()[0])

? ? ? #print(int(number.split()[0]))



? if largest != 0:

? ? print(largest, 'CAD')

? else:

? ? print("There are 0.")


A = ['10 cad', '9 cad', '7 cad', '12 cad' , '2 cad', '5 cad', '15 cad', '100 cad','17 cad','90 cad', '60 cad', '10 cad']

find_largest(A)


#? ? ? print(number.split()[0])


習(xí)題 1-3-2 split

# -*- coding: utf-8 -*-

"""

Created on Sat Jan? 7 09:40:34 2023


@author: pc

"""



money=input().split(', ')

total={}


for i in money:

? ? j=i.split()

? ? if total.get(j[1],0)==0:

? ? ? ? total[j[1]]=int(j[0])

? ? else:

? ? ? ? total[j[1]]+=int(j[0])

? ? ? ??

? ? ? ??

for i in total:

? ? print(total[i],i)



加拿大瑞爾森大學(xué) Python習(xí)題 Lab 1的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
会宁县| 盐源县| 开远市| 盖州市| 岳西县| 岱山县| 丽江市| 宜宾县| 林西县| 凤山市| 山阳县| 灌云县| 乐陵市| 永丰县| 辽阳市| 房产| 庄河市| 宾川县| 文水县| 曲麻莱县| 庆元县| 昌乐县| 乳源| 英山县| 尉犁县| 洛宁县| 尖扎县| 舒城县| 彝良县| 呈贡县| 台江县| 拉萨市| 汤原县| 江陵县| 新泰市| 于都县| 盘锦市| 辽宁省| 许昌县| 玉林市| 康定县|