Python Forum
Trouble converting a string into an int
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble converting a string into an int
#1
Hi team,

So I'm writing a simple program that asks for a user input of an integer, and then I need to convert that object into an argument that int() will accept so I can use bin() to retrieve the binary output but nothing I try seems to work. Incoming code:

#user inputs integer
Line 2: numb = input('Enter an integer >')
print(numb)
#numb here is a literal int string bound to the input of numb

numb =(input('Enter it again >' ))

#convert and bind object numb into a str
int(numb)
Line 9: bin1 = bin(numb)

#final output
print(bin1)


At line 9 I get the "builtins.Typeerror:'str' object cannot be interpreted as an integer" even though I thought I converted the object type previously...I'm stumped! I don't understand how the object 'numb' isn't converted into an int since the previous line shows no errors. Any ideas?
Reply
#2
You need to save the integer after you convert it
numb = int(numb)
Reply
#3
OMG you instantly solved that. I didn't realize I had to recall the numb object into the int() function. Thank you, good human.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting String to Integer Python Johnny1998 5 3,090 Aug-02-2019, 08:13 PM
Last Post: Johnny1998
  Trouble with assigning a string value in conditional statement juliabrushett 1 2,554 Jun-16-2018, 06:21 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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