Download Introduction to Programming Mr. Weisswange Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming
Mr. Weisswange
Programming Assignment #4—while loops
1. The string method s.isalpha() returns True or False depending on whether string s consists
only of letters. Write a program that prompts the user to enter a letter and keeps prompting until a
single letter is entered, like this:
Enter a letter: 5
Not a letter, try again: LETTER
Not a letter, try again: Q
You have entered the letter “Q”.
2. Write a program which reads integers from the user, ending when a negative number is entered.
After that, print: the number of entries, the sum of the entries, and the average of the entries. Keep in
mind that this program will require two accumulators, both of which need to be updated for each
iteration of the loop.
Enter a negative number to end.
Enter a number: 12
Enter a number: 15
Enter a number: 19
Enter a number: -1
Total number of entries: 3
Sum of entries: 46
Average of entries: 15.3333333
3. Write a program which reads in a starting value and an ending value and prints a table of squares
and square roots as follows, with table headings. Remember that printing the special “\t” character
moves over a tab:
Enter the starting value: 3
Enter the ending value: 8
Number Square Square root
3
9
1.73205
4
16
2
5
25
2.23607
6
36
2.44949
7
49
2.64575
8
64
2.82843
(go on to next page)
4. Write a program which plays a number guessing game with the user. Have the computer select a
number at random in the range 1 to 100 using the code shown below:
import random
secretNumber = random.randint(1,100)
Then, have the user repeatedly guess numbers, telling them whether each guess is too low or too
high, until the user gets the correct number. The program must tell the user how many guesses they
took. The output might look like this:
Guess a number between 1 and 100!
Your guess: 50
Too high!
Your guess: 30
Too low!
Your guess: 40
Too low!
Your guess: 43
You got it in 4 tries!
[Thought question: for 100 possible numbers, what is the maximum number of guesses that it would
take to get any secret number? For 1,000,000 possible numbers?]