Home > Software design >  new to python having trouble printing 4 variables
new to python having trouble printing 4 variables

Time:02-08

adding = int(5 3)
subtract = int(10-2)
multiplication = int(2*4)
division = int(16/2)
print (str(adding,"\n",subtract,"\n",multiplication,"\n",division))

im getting a typeError: TypeError: str() takes at most 3 arguments (7 given)

CodePudding user response:

Solution: Just change this piece of code from this

print (str(adding,"\n",subtract,"\n",multiplication,"\n",division))

To this.

print (adding,"\n",subtract,"\n",multiplication,"\n",division)

Reason: str() is a typecast method. You don't need to typecast each and everything.

For Reference: https://www.geeksforgeeks.org/type-casting-in-python-implicit-and-explicit-with-examples/

CodePudding user response:

Think of str as a function that takes in a variable and converts it into a string. Passing multiple variables to str() separated by comma will not apply string function to each variable. It will instead mean that they are arguments to the function. If you want to convert each variable into

adding = str(int(5 3))
subtract = str(int(10-2))
multiplication = str(int(2*4))
division = str(int(16/2))
print (adding,"\n",subtract,"\n",multiplication,"\n",division)

Note that print function accepts multiple arguments for printing purposes.

CodePudding user response:

To convert an integer to string, you need to use str() each variables separately. Also, you can use as concat operator to avoid extra space.

print(str(adding) "\n" str(subtract) "\n" str(multiplication) "\n" str(division))
  •  Tags:  
  • Related