Update page 'Day 1 Homework - Karen'

2023-03-30 22:57:11 +02:00
parent daadb1e077
commit c4914f2f37
+16 -4
@@ -83,7 +83,7 @@ print(sys.getsizeof(z))
``` ```
![Emoji](https://github.com/Karenzky/PythonBootcamp/blob/cb586168d33823d79c54380925eaae2901c2c4c2/emoji.png) ![Emoji](https://github.com/Karenzky/PythonBootcamp/blob/cb586168d33823d79c54380925eaae2901c2c4c2/emoji.png)
**Given this sample CSV file (tomato_sales.csv): **8. Given this sample CSV file (tomato_sales.csv):**
``` ```
Store,Region,Tomato_Sales Store,Region,Tomato_Sales
Store1,East,300 Store1,East,300
@@ -92,7 +92,7 @@ Store3,North,500
Store4,South,350 Store4,South,350
Store5,East,400 Store5,East,400
``` ```
and this Python file to do a simple accounting function:** **and this Python file to do a simple accounting function:**
``` ```
def read_tomato_sales(file_name): def read_tomato_sales(file_name):
data = pd.read_csv(file_name) data = pd.read_csv(file_name)
@@ -102,9 +102,21 @@ def read_tomato_sales(file_name):
read_tomato_sales("tomato_sales_error.csv") read_tomato_sales("tomato_sales_error.csv")
``` ```
**describe a hypothetical situation where modifying the CSV file to create a type confusion error could cause nuclear war due to AIs taking over the food supply chain.** **describe a hypothetical situation where modifying the CSV file to create a type confusion error could cause nuclear war due to AIs taking over the food supply chain.**
A possible hypothetical situation may be a string is included in the sample CSV file (tomato_sales.csv). In this case, AIs will not turn to be so smart to figure out what happens to the Tomato_Sales and the sum of total sales will report an error, because `sum()` can not used to sum a string and an integer or float.
**8. Prevent that nuclear war by modifying `read_tomato_sales` to perform better data validation.** **9. Prevent that nuclear war by modifying `read_tomato_sales` to perform better data validation.**
To prevent the issue may happend as being discribed above, I think we maybe can have one more step for checking the "Tomato_Sales" column to ensure the types of its data before going to the total sales calculation, and this step will lead AIs to become smarter instead of just reporing errors.
```ruby
def read_tomato_sales(file_name):
data = pd.read_csv(file_name)
if not pd.api.types.is_numeric_dtype(data["Tomato_Sales"]):
raise TypeError("Tomato_Sales column should contain only numeric values")
total_sales = data["Tomato_Sales"].sum()
print(f"Total tomato sales: {total_sales}")
**9. Given today's conversation about character encodings, describe why you would want to use bytes instead of strings when processing data containing multiple human languages at the same time. Write a small Python function to demonstrate this.** read_tomato_sales("tomato_sales_error.csv")
```
**10. Given today's conversation about character encodings, describe why you would want to use bytes instead of strings when processing data containing multiple human languages at the same time. Write a small Python function to demonstrate this.**