Objectives
- Solve problems using Python repetition
- Use Python while statements
- Use Python for statements
- Use Python nested repetition
- Validate user input
i = 1 while i <= 10: print(i) i = i + 1
sum = 0 strIn = input("Enter number to add (QUIT to exit): ") while strIn.upper() != "QUIT": num = float(strIn) sum = sum + num strIn = input("Enter number to add (QUIT to exit): ") else: print("The sum is %f" % sum)
valid = False while not valid: n = int(input("Enter an integer from 1 through 10: ")) if n < 1: print("Error: Below minimum value of 1"); elif n > 10: print("Error: Above maximum value of 10"); else: valid = True print("You entered: %d" % n)
for letter in "CIS 111": print(letter)
for num in [1, 7, 12, -4, 5]: print(num)
for num in 1, 7, 12, -4, 5: # parentheses optional around tuple print(num)
sum = 0 for n in range(1, 101, 2): # 1, 3, ..., 99 sum = sum + n print("The sum is %f" % sum)