加拿大瑞爾森大學(xué) Python習(xí)題 Lab 1
前段時(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)