Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am new at using python and would like some help on fixing my some code to work. Instructions 1: Replace the while below with

I am new at using python and would like some help on fixing my some code to work.

Instructions 1:

Replace the while below with a for loop by using the built-in function range, which consumes a number and returns a list up to that number. For example, range(5) returns the list [0,1,2,3,4]. In the end, the code should print out the sum of the numbers from 0 to 100.

Code I tried:

for index in range(100): print(index)

Error I got:

You should only be printing once.

Instructions 2:

Use a while loop to repeatedly take input from the user and print whatever they type in. When the user enters the empty string (""), then your program should stop.

Note that if your program accidentally loops forever, your browser may become unresponsive for up to 15 seconds. Be very careful while writing while loops!

Code I tried:

while True: reply = input('enter text, [type """" to quit]:') print (reply) if reply == '""': break

Error I got:

You will not need to use the comparison operator. Take advantage of the truthiness of the user input variable. When is a string True?

Instructions 3:

A scientist has been recording observations of the temperature for the past while. On hot days, they write "H", and for cold days, they write "C". Instead of using a list, they kept all of these observations in a single long string (e.g., "HCH" would be a hot day, followed by a cold day, and then another hot day).

Write a function add_hot_cold that consumes a string of observations and returns the number of hot days minus the number of cold days. To accomplish this, you should process each letter in turn and add 1 if it is hot, and -1 if it is cold. For example, the string "HCH" would result in 1, while the string "CHCC" would result in -2.

You cannot use the built-in count method or the len function. Call your function on the string "HHHHHCCCHCHHHCHCHC" and print the result.

This is probably the one i struggle with the most.

Any help or solution would be helpful.

Thanks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions

Question

a. Describe the encounter. What made it intercultural?

Answered: 1 week ago