0% found this document useful (0 votes)
30 views

Python_Worksheet_5_While_loops (1)

Uploaded by

an74829001
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Python_Worksheet_5_While_loops (1)

Uploaded by

an74829001
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Worksheet 5 While loops

Introduction to Python

Worksheet 5: While loops


Below is an example of the pseudocode and the corresponding code for a simple program to
add up numbers entered by a user and display the total.

Adding Program
count  0
runningTotal  0
input number
while number <> 0
count  count + 1
runningTotal  runningTotal + number
input next number
display count
display runningTotal

Compare the pseudocode above to the Python code below for the planned program. Use
this to help you write your own program using the while loop.
#Adder
print("Keeping a running total")
print("***********************\n")
print("Enter numbers to add together then press 0 to exit.")

#Initialise the variables


count = 0
runningTotal = 0
number = int(input("Input the first number: "))

#Add each number together until 0 is entered


while number != 0:
count = count + 1 # (or count+=1)
runningTotal = runningTotal + number
number = int(input("Input next number (0 to finish): "))

#Display the total sum and the number of entries made


print ("You entered ",count," numbers")
print ("Total = ", runningTotal)

input("Press ENTER to exit")

Challenge (for final assessment portfolio):


Plan and write a program called GuessMyNumber.py where the computer generates a
random number between 1 and 100 and the user must guess what it is. The program should
indicate whether they are too high or too low and display the final number of attempts at the
end.

You might also like