-
Notifications
You must be signed in to change notification settings - Fork 1
/
day4morning-help.txt
89 lines (68 loc) · 4.52 KB
/
day4morning-help.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Day 4 Morning Exercise – Conversion Calculator
Write Python code that converts a given value from its original unit to a different unit.
You will first need to make a New Notebook in Jupyter or Colab that runs Python 3.
In today's folder, there is a file called conversionMeasures.csv. It contains lines of data. Each line has three pieces of data, separated by commas: unit 1, conversion factor, unit 2.
The conversion equation for all of these lines is unit 1 x conversion factor = unit 2.
Your code should be able to convert the following test samples:
test_unit = "pint"
test_value = 2.5
final_unit = "mL"
test_unit = "cubic foot"
test_value = 30
final_unit = "liter"
test_unit = "slug"
test_value = "4.8" Yes, you should write your code to handle values that are entered as strings
final_unit = "pound"
test_unit = "slug"
test_value = 27.0
final_unit = "snail" - See 'Errors to anticipate' below
Your code should:
• store the conversion data from the csv file in some object (list of lists, dictionary of dictionaries, etc.)
• open the file in read mode
• save as a list of lines with f.readlines()
• exit the with/as statement
• create an empty list
• loop through the lines from the file
• for each line, remove the trailing new line character and split the line on the commas, which will make a list
• append the list to the empty list you made above – you should have a list of lists
• provide a way to find the correct conversion factor from your data object
• loop though the list of lists
• if the test_unit is equal to the item in the first position of the list and the final_unit is equal to the third item in the list, save the second item in the list and break the loop
• include a function to convert between units
• the function should take the test_value and conversion factor as arguments and return the new value
• call the function with your test_value and conversion factor
• print out a full sentence response with the final answer
• anticipate some errors (see below)
• run your code on the provided test examples
Errors to anticipate:
1. Someone might give the initial value as a string instead of a float/integer.
2. Someone might request a final unit that is not in your data – your code should print out an error message. Here’s a sample to test for this error:
test_unit = "slug"
test_value = 27.0
final_unit = "snail"
Tips:
• You might write out the steps you need to complete each task before you start coding
• Take it one step at a time
• If you are stuck, there is another version of this document that contains more detailed steps (thursdayHW-Help)
IF YOU ARE USING GOOGLE COLAB ONLY:
To upload the csv file, you will need to run a cell at the top of your notebook that says:
!wget https://raw.githubusercontent.com/nuitrcs/pythonBootcamp_4Day/main/conversionMeasures.csv
You can just copy and paste that line into a new code cell.
BONUS CHALLENGES
1. Someone might give a test or final unit that has different capitalization than how it is presented in the csv file. Edit your code so that it can still process this sample:
test_unit = "KM/H"
test_value = 8.4
final_unit = m/Sec
2. In the csv file, not all the units are included on both sides of the conversion factor. Someone might give you a test unit from the right side of the factor and ask you to convert it to the unit on the left side, which would require division instead of multiplication. Edit your code so that it can process this sample:
test_unit = "ergs"
test_value = 8.4
final_unit = "joule"
3. Advanced Challenge: there’s a function called input() that can collect data from the user of your code in real time. Here’s a link to a website that works through the input() function (top half of the page only – stop before the Tkinter section): https://datatofish.com/input-function-python/. Try to use the input() function to collect the test_unit, test_value, and final_unit values.
Python notebook and script organization
Code should be organized in the following order:
1. A description of what the notebook or script does, what type of data you must or can use (file type, required columns, etc.), and what products (files, visualizations, reports, etc.) are made.
2. import any packages used
3. define any input or output filenames, saving the file paths as variables
4. any data structures that will be used in the code (dictionaries, lists, etc.)
5. define any custom functions or objects that will be used in the body of the code
6. body of the code (calling functions, looping, cleaning data, doing calculations, etc.)