2. Use a while loop to sum all numbers from 1 to n.
3. Print the total.
Right Answer:
n = 5
sum =0
count=0
while count <=n:
sum+=count
count+=1
print(sum)
Right Answer:
n = 5
sum =0
count=0
while count <=n:
sum+=count
count+=1
print(sum)
Right Answer:
n = int(input("Enter a starting number: "))
while n<= 1000:
print(n)
n *= 2
Right Answer:
n = int(input("Enter a number: "))
sequence = [n]
while n != 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
sequence.append(n)
print("Collatz sequence:", sequence)
Right Answer:
n = int(input("Enter a number: "))
count = 1
while count <= n:
if count % 5 != 0:
print(count)
count += 1
Right Answer:
n = int(input("Enter a number: "))
count = 2
total = 0
while count <= n:
total += count
count += 2
print("Total:", total)
Right Answer:
integer_list = [4,8,25,4,69,2,78,-5,0,3,6]
index = 0
total = 0
while index < len(integer_list) and integer_list[index] >= 0:
total += integer_list[index]
index += 1
print(f"The sum is: {total}")
Right Answer:
n=5
while n >=1:
print(n)
n-=1
Right Answer:
number = 6
attempts = 0
while attempts < 3:
guess = int(input("Guess a number between 1 and 10: "))
if guess == number:
print("Congratulations! You guessed correctly!")
break
attempts += 1
if attempts == 3:
print("Sorry, you've run out of attempts. Better luck next time!")
Right Answer:
n=6
count =1
while count
count +=1