![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Posts: 11
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2007
Posts: 11
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2007
Posts: 11
Rep Power: 0
![]() |
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 + 2Last edited by DaWei; Nov 19th, 2007 at 1:25 AM. Reason: Added code tags. |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
Re: Need help with python
You're getting there.
Let's look at what you've done: Line 1: my_string = raw_input("Billy: ")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: ")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 my_string2 = raw_input("Please Enter Another String: ")
my_added_string = my_string + my_string2
print my_added_stringLine 4: my_added_integer = my_integer + 2 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. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Nov 2007
Posts: 11
Rep Power: 0
![]() |
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 Last edited by DaWei; Nov 19th, 2007 at 1:26 AM. Reason: Added code tags. |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Nov 2007
Posts: 11
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Nov 2007
Posts: 11
Rep Power: 0
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Perfect Python | Dietrich | Python | 30 | Jan 5th, 2007 12:38 PM |
| [tutorial] Python for programming beginners | coldDeath | Python | 30 | Dec 14th, 2005 11:35 AM |
| Convert Python script to C++ code | clanotheduck | Python | 17 | Sep 25th, 2005 8:55 AM |
| Advanced Python Tricks | Arevos | Python | 19 | Sep 24th, 2005 7:39 AM |
| Python - A Programmers Introduction | coldDeath | Python | 17 | Aug 19th, 2005 12:41 PM |