Home > Net >  Write a loop that asks the user to enter a number
Write a loop that asks the user to enter a number

Time:10-03

Write a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered.

I did this and I keep getting 45 instead of 55 why

num = 0
for i in range(10):
 float(input("enter a number: "))
 num  = i

print("total is" , num)

CodePudding user response:

change your code to.

num = 0
for i in range(10):
 num  = float(input("enter a number: "))

print("total is" , num)

CodePudding user response:

@BeeFriedman answer is good, but I prefer sum with a generator:

num = sum(float(input("enter a number: ")) for _ in range(10))
print("total is" , num)
  • Related