'i don't get it. why i just cant use int('something) in this code? why i need to make another variable like value = float('something)?
1st one is ok. but in second one it showed:ValueError: invalid literal for int() with base 10: 'done'
count = 0
total = 0
while True :
inp =input('enter number : ')
if inp == 'done': break
value = float(inp)
count = count + 1
total = total + value
print(count)
print(total)
print(total/count)
2nd :
count = 0
total = 0
while True :
inp =int(input('enter number : '))
if inp == 'done': break
count = count + 1
total = total + inp
print(count)
print(total)
print(total/count)
Solution 1:[1]
Right here
inp =int(input('enter number : '))
You are casting your input to int. So when you type 'done' to console, you get error, because it's not a valid integer
Solution 2:[2]
int('done') isn't a integer. So the int() function returns an exception.
Solution 3:[3]
inp is an integer. Therefore, you can't ask whether it's equal to a string('done'), because you're comparing different things.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sudden_appearance |
| Solution 2 | Bill Lynch |
| Solution 3 | Nathan Wolf |
