Posts: 14
Threads: 9
Joined: Jun 2018
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)
Posts: 8,160
Threads: 160
Joined: Sep 2016
Jul-02-2018, 03:46 AM
(This post was last modified: Jul-02-2018, 03:58 AM by buran.)
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()
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 14
Threads: 9
Joined: Jun 2018
Jul-02-2018, 03:59 AM
(This post was last modified: Jul-02-2018, 04:08 AM by juliabrushett.)
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)
Posts: 8,160
Threads: 160
Joined: Sep 2016
Jul-02-2018, 05:14 AM
(This post was last modified: Jul-02-2018, 05:14 AM by buran.)
@ 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.?
Posts: 14
Threads: 9
Joined: Jun 2018
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)
Posts: 8,160
Threads: 160
Joined: Sep 2016
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...
Posts: 4,220
Threads: 97
Joined: Sep 2016
(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.
Posts: 232
Threads: 2
Joined: Sep 2017
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
|