Reputation: 11
Im trying to do a small program but it wont work, I want to write 4 different numbers intenger and decimals, but it doesnt work. I get the error message TypeError: Can't convert 'int' object to str implicitly. Can you guys help me?
a = str(input("Ange ett siffervärde: "))
b = str(input("Ange ett siffervärde: "))
c = str(input("Ange ett siffervärde: "))
d = str(input("Ange ett siffervärde: "))
l = (a+b+c+d)
for l in range (a, d+1):
print(l)
I want the program to print the numbers I entered under each other.
Upvotes: 1
Views: 3153
Reputation: 967
a = int(input("a=: "))
b = int(input("b=: "))
lst = [a, b]
for number in lst:
print(number)
It's ok in my machine(python 2.6);
Upvotes: 1
Reputation: 1121972
You are trying to use d
as an integer:
for l in range (a, d+1):
by adding 1
to it, but you made it a string:
d = str(input("Ange ett siffervärde: "))
Make all your inputs integers instead:
a = int(input("Ange ett siffervärde: "))
b = int(input("Ange ett siffervärde: "))
c = int(input("Ange ett siffervärde: "))
d = int(input("Ange ett siffervärde: "))
Next, your for
loop clobbers the l
variable:
l = (a+b+c+d)
for l in range (a, d+1):
It isn't clear what you want to do in the loop, but the sum of a
, b
, c
and d
is now lost as l
is used as a loop variable too.
If you wanted to have decimals, you can use float()
instead of int()
, but note that range()
can only work with integers!
If you wanted to print the 4 numbers in a loop, then create a list first and loop directly over the list:
a = float(input("Ange ett siffervärde: "))
b = float(input("Ange ett siffervärde: "))
c = float(input("Ange ett siffervärde: "))
d = float(input("Ange ett siffervärde: "))
lst = [a, b, c, d]
for number in lst:
print(number)
or combine looping with asking for the number and printing it:
lst = []
for count in range(4):
number = float(input("Ange ett siffervärde: "))
print(number)
lst.append(number)
This asks for a number four times, prints the given number, and then adds that number to a list for later use.
Upvotes: 2
Reputation: 77902
Your variables a, b, c and d are strings, not numbers, so you cannot pass them as arguments to range
nor add them to ints.
Upvotes: 1
Reputation: 3520
Use this:
a = int(input("Ange ett siffervrde: "))
b = int(input("Ange ett siffervrde: "))
c = int(input("Ange ett siffervrde: "))
d = int(input("Ange ett siffervrde: "))
Upvotes: 1