Chapter 8 Writing Code: Hands-On Exercise
Now it’s your turn to try!
8.1 Code
Here’s the code you need for question #2.
def calculate_average(numbers):
total = 0
for number in numbers:
total += number
average = total / len(numbers)
return average
numbers = [1, 2, 3, 4, "5"]
average = calculate_average(numbers)
print("The average of the numbers is:", average)
8.2 Questions
Write a prompt to create a function that replaces all instances of the string “three” with the number 3, then create a prompt that allows you to modify the function so that “three”, “Three”, and “THREE” are all replaced with the number 3.
Write a query that helps you debug the code snippet from above.
Create a query that explains how to retrieve data from an online database. Don’t forget to find out what the source of the code is.
Write a prompt to plan out a program that creates and plays a game of Rock, Paper, Scissors. You should create a multi-step plan as well as figure out what your first coding task should be.
Create a prompt that identifies the best coding language for the above game.
## ─ Session info ───────────────────────────────────────────────────────────────
## setting value
## version R version 4.3.2 (2023-10-31)
## os Ubuntu 22.04.4 LTS
## system x86_64, linux-gnu
## ui X11
## language (EN)
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz Etc/UTC
## date 2024-09-04
## pandoc 3.1.1 @ /usr/local/bin/ (via rmarkdown)
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date (UTC) lib source
## askpass 1.2.0 2023-09-03 [1] RSPM (R 4.3.0)
## bookdown 0.39.1 2024-06-11 [1] Github (rstudio/bookdown@f244cf1)
## bslib 0.6.1 2023-11-28 [1] RSPM (R 4.3.0)
## cachem 1.0.8 2023-05-01 [1] RSPM (R 4.3.0)
## cli 3.6.2 2023-12-11 [1] RSPM (R 4.3.0)
## devtools 2.4.5 2022-10-11 [1] RSPM (R 4.3.0)
## digest 0.6.34 2024-01-11 [1] RSPM (R 4.3.0)
## ellipsis 0.3.2 2021-04-29 [1] RSPM (R 4.3.0)
## evaluate 0.23 2023-11-01 [1] RSPM (R 4.3.0)
## fansi 1.0.6 2023-12-08 [1] RSPM (R 4.3.0)
## fastmap 1.1.1 2023-02-24 [1] RSPM (R 4.3.0)
## fs 1.6.3 2023-07-20 [1] RSPM (R 4.3.0)
## glue 1.7.0 2024-01-09 [1] RSPM (R 4.3.0)
## hms 1.1.3 2023-03-21 [1] RSPM (R 4.3.0)
## htmltools 0.5.7 2023-11-03 [1] RSPM (R 4.3.0)
## htmlwidgets 1.6.4 2023-12-06 [1] RSPM (R 4.3.0)
## httpuv 1.6.14 2024-01-26 [1] RSPM (R 4.3.0)
## httr 1.4.7 2023-08-15 [1] RSPM (R 4.3.0)
## jquerylib 0.1.4 2021-04-26 [1] RSPM (R 4.3.0)
## jsonlite 1.8.8 2023-12-04 [1] RSPM (R 4.3.0)
## knitr 1.47.3 2024-06-11 [1] Github (yihui/knitr@e1edd34)
## later 1.3.2 2023-12-06 [1] RSPM (R 4.3.0)
## lifecycle 1.0.4 2023-11-07 [1] RSPM (R 4.3.0)
## magrittr 2.0.3 2022-03-30 [1] RSPM (R 4.3.0)
## memoise 2.0.1 2021-11-26 [1] RSPM (R 4.3.0)
## mime 0.12 2021-09-28 [1] RSPM (R 4.3.0)
## miniUI 0.1.1.1 2018-05-18 [1] RSPM (R 4.3.0)
## openssl 2.1.1 2023-09-25 [1] RSPM (R 4.3.0)
## ottrpal 1.2.1 2024-06-11 [1] Github (jhudsl/ottrpal@828539f)
## pillar 1.9.0 2023-03-22 [1] RSPM (R 4.3.0)
## pkgbuild 1.4.3 2023-12-10 [1] RSPM (R 4.3.0)
## pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 4.3.0)
## pkgload 1.3.4 2024-01-16 [1] RSPM (R 4.3.0)
## profvis 0.3.8 2023-05-02 [1] RSPM (R 4.3.0)
## promises 1.2.1 2023-08-10 [1] RSPM (R 4.3.0)
## purrr 1.0.2 2023-08-10 [1] RSPM (R 4.3.0)
## R6 2.5.1 2021-08-19 [1] RSPM (R 4.3.0)
## Rcpp 1.0.12 2024-01-09 [1] RSPM (R 4.3.0)
## readr 2.1.5 2024-01-10 [1] RSPM (R 4.3.0)
## remotes 2.4.2.1 2023-07-18 [1] RSPM (R 4.3.0)
## rlang 1.1.4 2024-06-04 [1] CRAN (R 4.3.2)
## rmarkdown 2.27.1 2024-06-11 [1] Github (rstudio/rmarkdown@e1c93a9)
## sass 0.4.8 2023-12-06 [1] RSPM (R 4.3.0)
## sessioninfo 1.2.2 2021-12-06 [1] RSPM (R 4.3.0)
## shiny 1.8.0 2023-11-17 [1] RSPM (R 4.3.0)
## stringi 1.8.3 2023-12-11 [1] RSPM (R 4.3.0)
## stringr 1.5.1 2023-11-14 [1] RSPM (R 4.3.0)
## tibble 3.2.1 2023-03-20 [1] CRAN (R 4.3.2)
## tzdb 0.4.0 2023-05-12 [1] RSPM (R 4.3.0)
## urlchecker 1.0.1 2021-11-30 [1] RSPM (R 4.3.0)
## usethis 2.2.3 2024-02-19 [1] RSPM (R 4.3.0)
## utf8 1.2.4 2023-10-22 [1] RSPM (R 4.3.0)
## vctrs 0.6.5 2023-12-01 [1] RSPM (R 4.3.0)
## xfun 0.44.4 2024-06-11 [1] Github (yihui/xfun@9da62cc)
## xml2 1.3.6 2023-12-04 [1] RSPM (R 4.3.0)
## xtable 1.8-4 2019-04-21 [1] RSPM (R 4.3.0)
## yaml 2.3.8 2023-12-11 [1] RSPM (R 4.3.0)
##
## [1] /usr/local/lib/R/site-library
## [2] /usr/local/lib/R/library
##
## ──────────────────────────────────────────────────────────────────────────────