Exercise 1:
a = ""
while a != "q" and a != "Q":
a = input("Please input a letter(q or Q to quit)")
print(a)
print("Loop ended")
Exercise 2:
print("5000 meters racing.")
for i in range(5):
print("running....")
print("Finished ",(i+1)*1000," meter")
answer = input("Will you carry on?")
if answer == "yes":
print("Go on running")
else:
print("Give up early")
break
print("Running end")
Exercise 3:
sum = 0
student_num = 0
while True:
score = input("Please input its score(input q or Q to quit)")
if score == "q" or score == "Q":
print("Input over. Quit")
break
if int(score) < 0:
print("Invalid input")
continue
student_num += 1
sum += int(score)
else:
print("quit")
print("Total input ",student_num," students")
print("The average score of the students is: ",sum/student_num)
Homework
import random as r
count = 0 #Used to record the valid guess times
for i in range(5):
number = r.randint(0,10)
print("Computer generate a random number 0~9")
answer = int(input("Please guess it"))
if answer < 0 or answer > 10:
print("You guess is not correct. Try one more time.")
continue
count += 1 #guess times increase
if answer == number:
print("Congratulation. You are right.",end = "")
break
print("Your guess wrong.")
else:
print("Sorry, good luck next time,",end = "")
print("You guess valid times are: ",count)
[…] loop| |____Unit 20 while loop practice| |____Unit 21 guess number game| |____Unit 22 for loop| |____Unit 23 break and continue| |____Unit 24 nested loop| |____Unit 25 Pycharm and turtle| |____Unit 26 mimic banking system I| […]