Python | Strings to Integers

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Python | Strings to Integers

Post by Prince Of The Elite on Fri Jun 10, 2011 6:09 am
([msg=58333]see Python | Strings to Integers[/msg])

Well, I've started learning python couple of weeks ago. I've become familiar with its structure and style. But, trying to make my very first and simple programe, I got some problems or in other words I lacked the code I needed, so can anyone offer help ....

1) is there a code that lets the user input an integer

2) a simmilar question, is there a code by which I can change strings into integers

3) well actualy it's not a request, it's a question: what does "raw_input" do ? And if it does differ from "input" what's the substitute in python 3 (i'm using python 3 and there's no such a code called raw_input)

That's all. I would be thankful to whoever help :-)
~~ ""Getting information off the internet is like taking a drink from a fire hydrant"" ~~


Prince Of The Elite
New User
New User
 
Posts: 26
Joined: Fri Jun 10, 2011 1:28 am
Blog: View Blog (0)


Re: Python | Strings to Integers

Post by JoeyPardella on Fri Jun 10, 2011 6:30 am
([msg=58334]see Re: Python | Strings to Integers[/msg])

hi i think the below article will answer most of your questions:

http://www.daniweb.com/software-develop ... eads/12326
JoeyPardella
Experienced User
Experienced User
 
Posts: 81
Joined: Tue Jan 04, 2011 8:43 am
Blog: View Blog (0)


Re: Python | Strings to Integers

Post by Prince Of The Elite on Fri Jun 10, 2011 7:33 am
([msg=58336]see Re: Python | Strings to Integers[/msg])

Well, thanks lot but it didn't fit my question ! Well, as i mentioned, im using python 3 and the "input" func. returns strings, only strings, just strings !!! In addition there isn't any explanation why doesn't the Py. func. include "raw_input" ....

Thanks though ;-)
~~ ""Getting information off the internet is like taking a drink from a fire hydrant"" ~~


Prince Of The Elite
New User
New User
 
Posts: 26
Joined: Fri Jun 10, 2011 1:28 am
Blog: View Blog (0)


Re: Python | Strings to Integers

Post by JoeyPardella on Fri Jun 10, 2011 7:37 am
([msg=58337]see Re: Python | Strings to Integers[/msg])

no not exactly, but input() was mentioned though ;)

here's something for your purpose:

http://www.wellho.net/resources/ex.php4 ... input_3.py

1st entry on google [random flame]
;)
JoeyPardella
Experienced User
Experienced User
 
Posts: 81
Joined: Tue Jan 04, 2011 8:43 am
Blog: View Blog (0)


Re: Python | Strings to Integers

Post by Prince Of The Elite on Fri Jun 10, 2011 8:11 am
([msg=58338]see Re: Python | Strings to Integers[/msg])

Ah that's what i mean now ! Thanx ....
~~ ""Getting information off the internet is like taking a drink from a fire hydrant"" ~~


Prince Of The Elite
New User
New User
 
Posts: 26
Joined: Fri Jun 10, 2011 1:28 am
Blog: View Blog (0)


Re: Python | Strings to Integers

Post by sanddbox on Fri Jun 10, 2011 10:08 pm
([msg=58351]see Re: Python | Strings to Integers[/msg])

Prince Of The Elite wrote:Well, I've started learning python couple of weeks ago. I've become familiar with its structure and style. But, trying to make my very first and simple programe, I got some problems or in other words I lacked the code I needed, so can anyone offer help ....

1) is there a code that lets the user input an integer

2) a simmilar question, is there a code by which I can change strings into integers


Code: Select all
while(True):
    input = raw_input("Please enter an integer: ")

    try:
        num = int(input)
    except ValueError:
        print "You entered invalid input."
        continue

    print "The number you entered is " + str(num)
   
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2331
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Python | Strings to Integers

Post by eggscrambler on Sun Nov 27, 2011 11:49 pm
([msg=63088]see Re: Python | Strings to Integers[/msg])

Prince Of The Elite wrote:1) is there a code that lets the user input an integer

2) a simmilar question, is there a code by which I can change strings into integers

3) well actualy it's not a request, it's a question: what does "raw_input" do ? And if it does differ from "input" what's the substitute in python 3 (i'm using python 3 and there's no such a code called raw_input)

Okay so here is the answer to your questions. Because you are using python 3.x there is no raw_input() just input() raw_input() was the python 2.x way of inputting a string and input() was how they made input for integers. But because of some problems with input() people would take the string that was entered into raw_input() and make it into a integer by typing int(raw_input()) or float(raw_input()). Keep in mind float() makes a number add .0 to the end so it can work with them and int() take whole numbers such as 5 . So for python 3.x instead of using raw_input() you use input() so to make it output floats or ints you would type int(input()) or float(input()). Hope that helps.
eggscrambler
New User
New User
 
Posts: 16
Joined: Thu Apr 28, 2011 11:30 pm
Blog: View Blog (0)


Re: Python | Strings to Integers

Post by Defience on Mon Nov 28, 2011 4:04 pm
([msg=63105]see Re: Python | Strings to Integers[/msg])

6 months old......R.I.P. :oops: and was already solved.
User avatar
Defience
Addict
Addict
 
Posts: 1281
Joined: Thu Jun 12, 2008 3:16 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests