1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2916710
  • 11
  • 1
  • 2

4 Answers4

2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

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: "))
Manoj Awasthi
  • 3,460
  • 2
  • 22
  • 26
  • If I use int I can't use decimals, – user2916710 Nov 12 '13 at 08:32
  • 1
    @user2916710: Then use `float`, but note that `range()` does not take floating point values. – Martijn Pieters Nov 12 '13 at 08:33
  • 1
    @user2916710: you need to tell us a lot more about what your program should **do** with the numbers here. – Martijn Pieters Nov 12 '13 at 08:33
  • I want it to work when I use decimals too like this, 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: ")) l = (a+b+c+d) for l in range (a, d+1): print(l*1.0) – user2916710 Nov 12 '13 at 08:45
1

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.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
1
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);

wshcdr
  • 935
  • 2
  • 12
  • 27