In: Computer Science
Sample session:
C:> python whileloop3.py
Enter a number: 2
Counting with 'while' ... 1 2
total = 2
Enter a number: 2
Counting with 'for' ... 1 2
total = 4
Enter a number: 3
Counting with 'while' ... 1 2 3
total = 7
Enter a number: 3
Counting with 'for' ... 1 2 3
total = 10
Enter a number: 1
Counting with 'while' ... 0 1
total = 11
Enter a number: 1
Counting with 'for' ... 0 1
total = 12
Enter a number: 3
total = 15, exiting
the code:
def look(): p = int(input("Put a number: ")) i = p//2-1 while i < p: i = i+1 print(i ,end=" ") if __name__ == "__main__": look()
Here is code:
def look():
sum = 0
choice = 'y'
while choice == 'y':
p = int(input("Put a number: "))
i = p//2
sum += p
for it in range(i, p+1):
print(it, end=" ")
print("\nTotal = ", sum)
choice = input("Do you want to continue (n/y) : ")
if __name__ == "__main__":
look()
Output: