Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Need help with python (http://www.programmingforums.org/showthread.php?t=14509)

charleneherron Nov 18th, 2007 11:32 PM

Need help with python
 
Hi guys.

Just wondering if someone would be kind enough to help me with something I'm stuck on. I have 4 small programs to write in python. Each one gets progressively harder. I've never programmed in python before and was sort of trying to learn on an online tutorial. It didn't help much! lol

Anyway, I've completed the first one, and now I'm stuck on the second one. I've tried for over 2 hours to figure this out and I can't. All this code looks like a bunch of letters and characters that are in a certain order to make the code, but I don't know why.

Can someone help a girl out with what the code would look like? :)

Here is what I have to do:

PROGRAM 2 - USING INPUT

Write an algorithm for a program that would get (as input from the user), 2 string variables and 2 integer variables, then joins together and displays the combined strings, and finally multiplies the two numbers on a new line. Write, test and debug the program using Python.

SAMPLE OUTPUT (not including author/program information)

Input string 1? Billy
Input String 2? Bob
Input integer A? 23
Input integer B? 2
BillyBob
46


Thanks guys!

Sane Nov 18th, 2007 11:44 PM

Re: Need help with python
 
Getting string input is as simple as:
:

my_string = raw_input("Message Before String Input: ")

Likewise, integer input is as simple as:
:

my_integer = input("Message Before Integer Input: ")

"raw_input" is a standard function, which is a part of Python, which will give you user input in the form of a string. "input" is also a standard function, which will give you user input in the form of an integer. As you may already know, a "string" is a series of characters, and an "integer" is a number.

Adding together two strings uses the "+" operator. And adding together two integers also uses the "+" operator.

:

my_added_string = my_string + another_string
my_added_integer = my_integer + another_integer


How handy is that!?

Those are all of the pieces of the puzzle. Piece them together the right way and you should have it! Good luck!

charleneherron Nov 18th, 2007 11:59 PM

Re: Need help with python
 
That's the thing... this all still looks foreign to me. I'm not a programmer at all and I just can't grasp this stuff. This sucks cuz I have to do it. *sighs* Looks like I'm getting a 0.

Sane Nov 19th, 2007 12:09 AM

Re: Need help with python
 
Well, you don't have to get a zero. I'm not writing the code for you, but I basically gave you every piece of the puzzle. It's just a matter of arranging things correctly.

Take your time reading a tutorial. Try running pieces of the code I gave you. Try different things and see what happens. Try taking a logical approach and step through each line to see exactly what's happening.

When you write this program, think of it as if you're translating from one language to another. Just as English can be translated to Spanish, it can also be translated to Python, by finding out what commands and syntax (grammar) of Python match that of English. Look at it step by step, and figure out the Python for it.

For example... given some English:
:

my age is 45
my brother's age is 41
tell me the difference between our ages


The equivilent of 'is' in Python is '='.
The equivilent of telling the user something is 'print'.
A difference, mathematically, is a minus b.

That would give us the following Python code:
:

my_age = 45
brothers_age = 41
print my_age - brothers_age


Does that make sense? Try applying this to other things and concepts. See what you can get working. Work your way up to the other code I've been showing you.

Post some attempts. Someone can give you more direction if you show that you've tried something.

charleneherron Nov 19th, 2007 12:55 AM

Re: Need help with python
 
This is useless. I'm ready to beat the computer.
I've already read tutorials on this. I'm just not a programmer.

Here is what I have:
:

my_string = raw_input("Billy: ")
my_integer = input("23: ")
my_added_string = my_string + Billy
my_added_integer = my_integer + 2

I almost give up.

Sane Nov 19th, 2007 1:05 AM

Re: Need help with python
 
You're getting there.

Let's look at what you've done:

Line 1:
:

my_string = raw_input("Billy: ")
This prompts the user with the text "Billy: ", and stores the input as the variable "my_string".

You might want to prompt the user with some more appropriate text. Maybe "Enter A String".
:

my_string = raw_input("Enter A String: ")

Line 2:
:

my_integer = input("23: ")
This prompts the user for a number, with the text "23: ", and stores it as the variable my_integer.

You might want to prompt the user with some more appropriate text. Maybe "Enter A Number".
:

my_integer = input("Enter A Number: ")

Line 3:
:

my_added_string = my_string + Billy
This combines the two variables my_string and Billy. my_string is what the user entered. However, Billy does not exist. You have not defined or declared Billy as anything. You want to add my_string to something that is already defined, such as my_string2. Then define my_string2 the same way you defined my_string.
:

my_string2 = raw_input("Please Enter Another String: ")
my_added_string = my_string + my_string2
print my_added_string


Line 4:
:

my_added_integer = my_integer + 2
This adds 2 to my_integer, which is what the user inputted for his/her number. So if the user entered 6, my_added_integer will be 8. This code is fine, but doesn't do what you want. You'll want to change it similar to how you are going to change line 3.

Then... you also want to show the values to whoever's running the program. You can do that with "print". "print" outputs something to the screen. Like so:
:

print my_added_string
print my_added_integer


Try looking at this code:
:

number1 = 4
number2 = 6
number3 = number1 + number2
print number3


Run that. See if it behaves how you expected. See if you can use what you've learned from that in your code.

charleneherron Nov 19th, 2007 1:09 AM

Re: Need help with python
 
BTW - here is my first project. I followed the tutorials and did it okay.
:

print "2+2 =",2+2
print "4-2 =",4-2
print "4*2 =",4*2
print "4/2 =",4/2
print "2**3 =",2**3
print "5%2 =",5%2

But then the tutorials went on about something entirely different than what I have to do for the 2nd project.

charleneherron Nov 19th, 2007 1:16 AM

Re: Need help with python
 
Well, now I'm getting a subprocess error when opening Python telling me a firewall is blocking the connection. Funny, I don't have any firewalls installed. Well, I guess that's it until tomorrow. Thanks for trying to help, Sane.

Sane Nov 19th, 2007 1:18 AM

Re: Need help with python
 
IDLE (The Python Code Writing Area) is being bitchy. It's sometimes like that.

Chances are you wrote a program that prompted you for some text. You ran the program, and never entered any text.

You can fix that by closing it all, and reopening it again. Sometimes you have to do that with "ctrl + alt + delete" and ending all processes that are "pythonw.exe" and "python.exe". But that may be above your skill level.

Sorry I could not help any more. Best of luck.

charleneherron Nov 19th, 2007 1:22 AM

Re: Need help with python
 
I know how to end a process. Funny thing is I'm actually a computer geek - just don't know anything about programming. Can't seem to grasp it. Oh well.


All times are GMT -5. The time now is 3:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC