Text
stringlengths
1
9.41k
WHILE LOOPS_ code below. i = 0 **while i<50:** **print(i)** i=i+2 **print('Bye!')** The variable i gets set to 0 to start. Next, the program tests the condition on the while loop.
Because i is 0, which is less than 50, the code indented under the while statement will get executed.
This code prints the current value of i and then executes the statement i=i+2 which adds 2 to i. The variable i is now 2 and the program loops back to the while statement.
It checks to see if i is less than 50, and since i is 2, which is less than 50, the indented code should be executed again.
So we print i again, add 2 to it, and then loop back to check the while loop condition again.
We keep doing this until finally i gets to 50. At this point, the while condition will finally not be true anymore and the program jumps down to the first statement after the while, which prints Bye!.
The end result of the program is the numbers 0, 2, 4, ..., 48 followed by the message, Bye!. ###### 9.2 Infinite loops When working with while loops, sooner or later you will accidentally send Python into a neverending loop.
Here is an example: i=0 **while i<10:** **print(i)** In this program, the value of i never changes and so the condition i<10 is always true. Python will continuously print zeroes.
To stop a program caught in a never-ending loop, use Restart Shell under the Shell menu.
You can use this to stop a Python program before it is finished executing. Sometimes a never-ending loop is what you want.
A simple way to create one is shown below: **while True:** _# statements to be repeated go here_ The value True is called a boolean value and is discussed further in Section 10.2. ###### 9.3 The break statement The break statement can be used to break out of a for or while loop before the loop is finished. |de be...
Here is an example:|Col2| |---|---| ||| |i=0 while i<10: print(i)|| |metimes a never-ending loop is what you want.
A simple way to create one is shown below:|Col2| |---|---| ||| |while True: # statements to be repeated go here|| ----- _9.4.
THE ELSE STATEMENT_ 79 **Example 1** Here is a program that allows the user to enter up to 10 numbers.
The user can stop early by entering a negative number. **for i in range(10):** num = eval(input('Enter number: ')) **if num<0:** **break** This could also be accomplished with a while loop. i=0 num=1 **while i<10 and num>0:** num = eval(input('Enter a number: ')) Either method is ok.
In many cases the break statement can help make your code easier to understand and less clumsy. **Example 2** Earlier in the chapter, we used a while loop to allow the user to repeatedly enter temperatures to be converted.
Here is, more or less, the original version on the left compared with a different approach using the break statement. temp = 0 **while True:** **while temp!=-1000:** temp = eval(input(': ')) temp = eval(input(': ')) **if temp==-1000:** **if temp!=-1000:** **print('Bye')** **print(9/5*temp+32)** **break** **else:*...
The code indented under the **else gets executed only if the loop completes without a break happening.** **Example 1** This is a simple example based off of Example 1 of the previous section. |ample 1 Here is a program that allows the user to enter up to 10 numbers.
The user can stop ly by entering a negative number.|Col2| |---|---| ||| |for i in range(10): num = eval(input('Enter number: ')) if num<0: break|| |s could also be accomplished with a while loop.|Col2| |---|---| ||| |i=0 num=1 while i<10 and num>0: num = eval(input('Enter a number: '))|| |ifferent approach using the ...
WHILE LOOPS_ The program allows the user to enter up to 10 numbers. If they enter a negative, then the program prints Stopped early and asks for no more numbers.
If the user enters no negatives, then the program prints User entered all ten values. **Example 2** Here are two ways to check if an integer num is prime.
A prime number is a number whose only divisors are 1 and itself.
The approach on the left uses a while loop, while the approach on the right uses a for/break loop: i=2 **for i in range(2, num):** **while i<num and num%i!=0:** **if num%i==0:** i=i+1 **print('Not prime')** **if i==num:** **break** **print('Prime')** **else:** **else:** **print('Prime')** **print('Not prime')** ...
To see if a value i is a divisor of num, we just have to check to see if num%i is 0. The idea of the while loop version is we continue looping as long as we haven’t found a divisor.
If we get all the way through the loop without finding a divisor, then i will equal num, and in that case the number must be prime. The idea of the for/break version is we loop through all the potential divisors, and as soon as we find one, we know the number is not prime and we print Not prime and stop looping.
If we get all the way through the loop without breaking, then we have not found a divisor.
In that case the **else block will execute and print that the number is prime.** ###### 9.5 The guessing game, more nicely done It is worth going through step-by-step how to develop a program.
We will modify the guessing game program from Section 9.1 to do the following: - The player only gets five turns. - The program tells the player after each guess if the number is higher or lower. - The program prints appropriate messages for when the player wins and loses. Below is what we want the program to...
A prime number is a number ose only divisors are 1 and itself.
The approach on the left uses a while loop, while the approach the right uses a for/break loop:|Col2| |---|---| ||| |i=2 for i in range(2, num): while i<num and num%i!=0: if num%i==0: i=i+1 print('Not prime') if i==num: break print('Prime') else: else: print('Prime') print('Not prime')|| ----- _9.5.
THE GUESSING GAME, MORE NICELY DONE_ 81 ###### LOWER. 3 guesses left. Enter your guess (1-100): 12 LOWER. 2 guesses left. Enter your guess (1-100): 6 HIGHER.
1 guesses left. Enter your guess (1-100): 9 LOWER. 0 guesses left. You lose.
The correct number is 8 First, think about what we will need in the program: - We need random numbers, so there will be an import statement at the beginning of the program and a randint function somewhere else. - To allow the user to guess until they either guess right or run out of turns, one solution is to use...
As this is something that is repeatedly done, it will go inside the loop. - There will be an if statement to take care of the higher/lower thing.
As this comparison will be done repeatedly and will depend on the user’s guesses, it will go in the loop after the input statement. - There will be a counting variable to keep track of how many turns the player has taken.
Each time the user makes a guess, the count will go up by one, so this statement will also go inside the loop. Next start coding those things that are easy to do: **from random import randint** secret_num = randint(1,100) num_guesses = 0 **while #some condition goes here#** guess = eval(input('Enter your guess...
WHILE LOOPS_ **if guess < secret_num:** **print('HIGHER.', 5-num_guesses, 'guesses left.\n')** **elif guess > secret_num:** **print('LOWER.', 5-num_guesses, 'guesses left.\n')** **else:** **print('You got it!')** Finally, it would be nice to have a message for the player if they run out of turns.
When they run out of turns, the while loop will stop looping and program control will shift to whatever comes outside of the loop.
At this point we can print the message, but we only want to do so if the reason that the loop stopped is because of the player running out of turns and not because they guessed correctly.
We can accomplish this with an if statement after the loop.
This is shown below along with the rest of the completed program. **from random import randint** secret_num = randint(1,100) num_guesses = 0 guess = 0 **while guess != secret_num and num_guesses <= 4:** guess = eval(input('Enter your guess (1-100): ')) num_guesses = num_guesses + 1 **if guess < secret_num:** **pri...
At this point we can print the message, but we only want to do so if the reason t the loop stopped is because of the player running out of turns and not because they guessed rectly.
We can accomplish this with an if statement after the loop.
This is shown below along h the rest of the completed program.|Col2| |---|---| ||| |from random import randint secret_num = randint(1,100) num_guesses = 0 guess = 0 while guess != secret_num and num_guesses <= 4: guess = eval(input('Enter your guess (1-100): ')) num_guesses = num_guesses + 1 if guess < secret_num: prin...
The correct number is', secret_num)|| |re is an alternative solution using a for/break loop:|Col2| |---|---| ||| |from random import randint secret_num = randint(1,100) for num_guesses in range(5): guess = eval(input('Enter your guess (1-100): ')) if guess < secret_num: print('HIGHER.', 5-num_guesses, 'guesses left.\n...
The correct number is', secret_num)|| ----- _9.6. EXERCISES_ 83 ###### 9.6 Exercises 1. The code below prints the numbers from 1 to 50.
Rewrite the code using a while loop to accomplish the same thing. for i in range(1,51): print(i) 2.
(a) Write a program that uses a while loop (not a for loop) to read through a string and print the characters of the string one-by-one on separate lines. (b) Modify the program above to print out every second character of the string. 3.
A good program will make sure that the data its users enter is valid. Write a program that asks the user for a weight and converts it from kilograms to pounds.
Whenever the user enters a weight below 0, the program should tell them that their entry is invalid and then ask them again to enter a weight. [Hint: Use a while loop, not an if statement]. 4.
Write a program that asks the user to enter a password. If the user enters the right password, the program should tell them they are logged in to the system.
Otherwise, the program should ask them to reenter the password.
The user should only get five tries to enter the password, after which point the program should tell them that they are kicked off of the system. 5.
Write a program that allows the user to enter any number of test scores. The user indicates they are done by entering in a negative number. Print how many of the scores are A’s (90 or above).
Also print out the average. 6. Modify the higher/lower program so that when there is only one guess left, it says 1 guess, not 1 guesses. 7.
Recall that, given a string s, s.index('x') returns the index of the first x in s and an error if there is no x. (a) Write a program that asks the user for a string and a letter.
Using a while loop, the program should print the index of the first occurrence of that letter and a message if the string does not contain the letter. (b) Write the above program using a for/break loop instead of a while loop. 8.
The GCD (greatest common divisor) of two numbers is the largest number that both are divisible by. For instance, gcd(18,42) is 6 because the largest number that both 18 and 42 are divisible by is 6.
Write a program that asks the user for two numbers and computes their gcd. Shown below is a way to compute the GCD, called Euclid’s Algorithm. - First compute the remainder of dividing the larger number by the smaller number - Next, replace the larger number with the smaller number and the smaller number with...
The GCD is the last value of the larger number. ----- 84 _CHAPTER 9. WHILE LOOPS_ 9. A 4000-year old method to compute the square root of 5 is as follows: Start with an initial guess, say 1.
Then compute 1 + 1[5] = 3. 2 Next, take that 3 and replace the 1’s in the previous formula with 3’s . This gives 3 + [5]3 = 7/3 ≈ 2.33. 2 Next replace the 3 in the previous formula with 7/3.
This gives 7/3 + 75/3 = [47] 2 21 _[≈]_ [2.24.] If you keep doing this process of computing the formula, getting a result, and plugging it back _�_ in, the values will eventually get closer and closer to 5.
This method works for numbers other than 5. Write a program that asks the user for a number and uses this method to estimate the square root of the number correct to within 10[−][10].
The estimate will be correct to within 10[−][10] when the absolute value of the difference between consecutive values is less than 10[−][10]. 10.
Write a program that has a list of ten words, some of which have repeated letters and some which don’t.
Write a program that picks a random word from the list that does not have any repeated letters. 11.
Write a program that starts with a 5 5 list of zeroes and randomly changes exactly ten of _×_ those zeroes to ones. 12.
Write a program in which you have a list that contains seven integers that can be 0 or 1. Find the first nonzero entry in the list and change it to a 1.
If there are no nonzero entries, print a message saying so. 13. In Chapter 4 there was a problem that asked you to write a program that lets the user play Rock-Paper-Scissors against the computer.
In that program there were exactly five rounds. Rewrite the program so that it is a best 3 out of 5. That is, the first player to win three times is the winner. 14.
Write a program to play the following simple game. The player starts with $100. On each turn a coin is flipped and the player has to guess heads or tails.
The player wins $9 for each correct guess and loses $10 for each incorrect guess. The game ends either when the player runs out of money or gets to $200. 15.
Write a program to play the following game. There is a list of several country names and the program randomly picks one.
The player then has to guess letters in the word one at a time. Before each guess the country name is displayed with correctly guessed letters filled in and the rest of the letters represented with dashes.
For instance, if the country is Canada and the player has correctly guessed a, d, and n, the program would display -ana-da.
The program should continue until the player either guesses all of the letters of the word or gets five letters wrong. ----- _9.6. EXERCISES_ 85 16. Write a text-based version of the game Memory.
The game should generate a 5 5 board (see _×_ the exercise from Chapter 8). Initially the program should display the board as a 5 5 grid of _×_ asterisks.
The user then enters the coordinates of a cell. The program should display the grid with the character at those coordinates now displayed. The user then enters coordinates of another cell.
The program should now display the grid with the previous character and the new character displayed. If the two characters match, then they should permanently replace the asterisks in those locations.
Otherwise, when the user enters the next set of coordinates, those characters should be replaced by asterisks. The game continues this way until the player matches everything or runs out of turns.
You can decide how many turns they player gets. 17. Ask the user to enter the numerator and denominator of a fraction, and the digit they want to know.
For instance, if the user enters a numerator of 1 and a denominator of 7 and wants to know the 4th digit, your program should print out 8, because [1]7 [=][ .142856...][ and 8 is the 4th] digit.
One way to do this is to mimic the long division process you may have learned in grade school. It can be done in about five lines using the // operator at one point in the program. 18.
Randomly generate a 6 6 list that has exactly 12 ones placed in random locations in the list. _×_ The rest of the entries should be zeroes. 19.
Randomly generate a 9 9 list where the entries are integers between 1 and 9 with no repeat _×_ entries in any row or in any column. ----- 86 _CHAPTER 9.
WHILE LOOPS_ ----- ### Chapter 10 ## Miscellaneous Topics II In this chapter we will look at variety of useful things to know. ###### 10.1 str, int, float, and list The str, int, float, and list functions are used to convert one data type into another. ###### str Quite often we will want to convert a number to ...
The built-in function str is used to convert things into strings.
Here are some examples: Statement Result **str(37)** '37' **str(3.14)** '3.14' **str([1,2,3])** '[1,2,3]' ###### int and float The int function converts something into an integer.
The float function converts something into a floating point number.
Here are some examples. Statement Result **int('37')** 37 **float('3.14')** 3.14 **int(3.14)** 3 To convert a float to an integer, the int function drops everything after the decimal point. 87 ----- 88 _CHAPTER 10.
MISCELLANEOUS TOPICS II_ ###### list The list function takes something that can be converted into a list and makes into a list. Here are two uses of it. **list(range(5))** [0,1,2,3,4] **list('abc')** ['a','b','c'] ###### Examples **Example 1** Here is an example that finds all the palindromic numbers between 1 and...
A palindromic number is one that is the same backwards as forwards, like 1221 or 64546. **for i in range(1,10001):** s = str(i) **if s==s[::-1]:** **print(s)** We use the str function here to turn the integer i into a string so we can use slices to reverse it. **Example 2** Here is an example that tells a person b...
We use int to convert those characters into an integer so we can do math with the year. **Example 3** Write a program that takes a number num and adds its digits.
For instance, given the number 47, the program should return 11 (which is 4 + 7).
Let us start with a 2-digit example. digit = str(num) answer = int(digit[0]) + int(digit[1]) The idea here is that we convert num to a string so that we can use indexing to get the two digits separately.
We then convert each back to an integer using the int function.
Here is a version that handles numbers with arbitrarily many digits: digit = str(num) answer = 0 **for i in range(len(digit)):** answer = answer + int(digit[i]) We can do the above program in a single line using a list comprehension. |ample 1 Here is an example that finds all the palindromic numbers between 1 and 1...
A indromic number is one that is the same backwards as forwards, like 1221 or 64546.|Col2| |---|---| ||| |for i in range(1,10001): s = str(i) if s==s[::-1]: print(s)|| |ample 2 Here is an example that tells a person born on January 1, 1991 how old they are in 0.|Col2| |---|---| ||| |birthday = 'January 1, 1991' year =...
Let us start with a 2-digit example.|Col2| |---|---| ||| |digit = str(num) answer = int(digit[0]) + int(digit[1])|| |e idea here is that we convert num to a string so that we can use indexing to get the two digits arately.
We then convert each back to an integer using the int function.