Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Converting String to Integer Python
#1
NOTE: I am executing the Python langauge in Atom, a Text Editor for Coding.

I have a Homework assignment in which I'm supposed to get the following code to print out the expected output; 3.
a = "1"
b = 2
print(a + b)
I presume you have to convert the string; a, or "1" to a integer. Yet, when I change the above code to:
a = "1"
b = 2
int("1")
print(a + b)
(Which I assume is the correct way to do things?)

I get the following error:
Output:
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate str (not "int") to str
Any help would be greatly appreciated!

Thanks,
Johnny

@Yoriz
@ichabod801

Thanks! I will endeavor to not make this mistake again, Sorry!
Yoriz wrote Aug-02-2019, 07:29 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
ichabod801 wrote Aug-02-2019, 07:29 PM:
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.
Quote
#2
int('1') does not change the value a is assigned to. It creates a new value and converts that. You need to reassign the new value to a. And you should convert a, not a new value, to make sure it works if the value of a is changed.
Yoriz likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
using int is correct but you need to store the result from int to a variable.
you can use int on the variable a and store the result on a if you want to permanently change it.
Quote
#4
(Aug-02-2019, 07:31 PM)ichabod801 Wrote: int('1') does not change the value a is assigned to. It creates a new value and converts that. You need to reassign the new value to a. And you should convert a, not a new value, to make sure it works if the value of a is changed.

Thanks! This helped.

To solve the problem, I simply did as ichabod801 instructed. I changed the code
a = "1"
b = 2
int("1") 
print(a + b)
to this code:

a = "1"
b = 2
a = int("1") 
print(a + b)
Thanks Again!
Quote
#5
I would suggest using a = int(a) instead. That way it will still work if a is not '1'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#6
(Aug-02-2019, 08:04 PM)ichabod801 Wrote: I would suggest using a = int(a) instead. That way it will still work if a is not '1'.

Seems reasonable. I have added that to my code.

Thanks.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  week 1 python help - string formatting ? raymond2688 20 598 Jul-09-2019, 08:10 PM
Last Post: snippsat
  integer representing a 10-digit phone number critter70 5 608 May-17-2019, 09:29 AM
Last Post: saravanatn
  integer +-* interval mcgrim 2 211 May-08-2019, 11:53 AM
Last Post: mcgrim
  converting decimal to binary missus_brown 1 249 Feb-19-2019, 02:42 AM
Last Post: stullis
  Converting str to int or float juliabrushett 6 1,450 Jun-18-2018, 10:08 AM
Last Post: DeaD_EyE
  Converting user input to integers (retitled) rwood1875 1 781 Jun-17-2018, 04:15 AM
Last Post: Mekire
  time zone converting with list slicing isabelle 9 1,203 May-24-2018, 09:38 AM
Last Post: buran
  Integer object error Zatoichi 2 1,015 Feb-18-2018, 07:25 AM
Last Post: Zatoichi
  Help converting int to str value in loop statement Jrvelandia 2 753 Feb-12-2018, 08:57 AM
Last Post: Jrvelandia
  Python: Returning the most frequently occurring substring in a larger string sskkddrit 2 907 Feb-09-2018, 06:41 AM
Last Post: sskkddrit

Forum Jump:


Users browsing this thread: 1 Guest(s)