Python Forum
parameter in function being involuntarily converted to str??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parameter in function being involuntarily converted to str??
#1
My program is to take in two numbers from a user, then sum all the values shared between those two numbers. I am not sure why my program isn't running.

The error message that pops up is as follows:

Traceback (most recent call last):
File "C:\Users\Julia\Desktop\Python\Assignment11\Assignment11\Assignment11.py", line 24, in <module>
z = sumTo(num1, num2)
File "C:\Users\Julia\Desktop\Python\Assignment11\Assignment11\Assignment11.py", line 17, in sumTo
sum = sum + x
TypeError: unsupported operand type(s) for +: 'int' and 'str'

I'm super confused as to why 'y' is taking on str type instead of int.
Thanks in advance for your help!

def sumTo(x, y):
    sum = 0 

    if(x > y):
        temp = y
        y = x
        x = temp

    while(x <= y):
        sum = sum + x
        x = x + 1
    
    return sum

num1 = input("Enter one number: ")
num2 = input("Enter another number: ")
z = sumTo(num1, num2)
print("sum = ", z)
Reply
#2
input always return str. you never converted the user input to int.
A couple of side notes:
  • don't use sum as variable name. It's built-in function and you will not be able to use it.
  • There are better approaches to swap x and y

    x, y = sorted(x, y)
    or
    if x > y:
        x, y = y, x
  • There are better aproaches to solve the task, check range()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Which version are you using? In Python 3.x, input returns a string, always. It is the equivalent of the Python 2.x raw_input. You would need to convert them to integers using int.

Also, since this is in homework you may not have covered this yet, but the three lines under the if statement in sumTo can be shortened:

x, y = y, x
I'm not sure why you have x = x + 1, it won't do anything. Changing variables in a function only affect things outside the function for mutable data types, but integers (and strings) are immuntable.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Oh, thank you! I am still receiving this error message, though:

Traceback (most recent call last):
File "C:\Users\Julia\Desktop\Python\Assignment11\Assignment11\Assignment11.py", line 16, in <module>
x = input(int("Enter one number: "))
ValueError: invalid literal for int() with base 10: 'Enter one number: '


Here is my code now:
def sumTo(x, y):
    sum = 0
    if(x > y):
        x, y = y, x
    while(x <= y):
        sum = sum + x
    return sum

x = input(int("Enter one number: "))
y = input(int("Enter another number: "))
z = sumTo(x, y)
print("sum = ", z)

Now I've figured out the type issue, but my code stops running after I input the second number. Did I not call the function correctly?

def sumTo(x, y):
    sum = 0
    if(x > y):
        x, y = y, x
    while(x <= y):
        sum = sum + x
    return sum

x = input("Enter one number: ")
x = int(x)
y = input("Enter another number: ")
y = int(y)
z = sumTo(x, y)
print("sum = ", z)
Reply
#5
@ichabod801 did a mistake by suggesting to remove x = x + 1 line. Without it you are in infinite loop as x will never change and equal to y
Did you read my first post about sum, etc.?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Yes, I did, thank you. My sum is spitting out "0".

def sumTo(x, y):
    s = 0
    if(x > y):
        x, y = y, x
    else :
        x, y = y, x
    while(x <= y):
        s = s + x
        x += 1
    return s

x = input("Enter one number: ")
x = int(x)
y = input("Enter another number: ")
y = int(y)

z = sumTo(x, y)
print("sum =", z)
Reply
#7
why did you add the else part? It was not in your original code. at the moment it will work if x>y, but otherwise it will retirn 0, because you will never run the loop. if x is not greater than y you don't have to do anything...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Jul-02-2018, 05:14 AM)buran Wrote: @ichabod801 did a mistake by suggesting to remove x = x + 1 line. Without it you are in infinite loop as x will never change and equal to y

Yes. My bad.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Once you've removed the extraneous else, it should all work, but I urge you to get into the habit of using meaningful variable names in the future, as it will make it easier for you to remember what the code is doing when you return to it to reuse it or improve it, and it helps anyone else looking at your code as well.

Here's a version of your programme with better names. (I've also included some type hints - the : int and -> int bits, but you don't need to include them yet, although you are likely to see them in other people's code.)

def sum_to(start: int, fini: int) -> int:
    ''' return sum of integers from start to fini inclusive of both '''
    total = 0
    if start > fini:
        start, fini = fini, start
    for value in range(start, fini + 1):
        total += value
    return total
 
first = int(input("Enter one number: "))
second = int(input("Enter another number: "))
print(f'sum of {first} to {second} is: {sum_to(first, second)}')
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: only size-1 arrays can be converted to Python scalars RainbowTrish1 1 3,018 Oct-29-2020, 06:16 PM
Last Post: DeaD_EyE
  Image data cannot be converted to float rakeshd3 1 7,049 Mar-17-2020, 08:01 AM
Last Post: buran
  A function taking in parameter int and str GrosseGaro 1 2,348 Sep-25-2019, 06:20 PM
Last Post: ichabod801
  Recursive function with a parameter MOH 0 2,051 Apr-30-2018, 09:39 PM
Last Post: MOH
  function with radius as a parameter that returns area of a circle taydeal20 4 7,162 Feb-07-2018, 03:33 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020