Python Programming help-- Anyone can help a newb?

Post » Fri Jul 26, 2013 1:49 pm

Hello forumers!

I've recently been able to dedicate more time to learning programming (of which I chose to learn Python).

I know about booleans, loops, if statements, etc., but I don't think I understand the theory exactly, which is why my current program does not seem to do what I am intending it to do.

Here is a sample of what my code looks like:

---------------

print("xxxxxx")
print("xxxxx?")
variable= input()
while variable != yes:
print("xxxx")
if variable== yes:
print("xxxxx")
print("xxxx")
print("xxxxx")

---------------

Obviously the "xxx"s have particular strings in them (did I use the jargon correctly?), but I am withholding the message I intend my program to prompt.
I am practicing coding and I am relaying this program to my friend to demonstrate what I've learned, and practice.
So here is my objective: I start off with a statement (ie, observation) and the second print function asks a question. I want to program my code so that if yes is not returned, the program asks another questions, prompting the user to finally return yes.
Once yes is returned, I continue with two, closing prompts, and my program is finished.
It's simple, but it does require some fundamental concepts of Python. I've already made one, small interactive conversation & one, simple game (a guess the number game). So I am beginning to understand the fundamentals, yet I definitely need more practice.
Any help is greatly appreciated! :smile:
Also, seeing how Python is slowly growing a trend amongst new programmers, maybe I'll go ahead and open up a Python enthusiasts' thread! But we'll come to that later.
Thanks!
User avatar
Gemma Woods Illustration
 
Posts: 3356
Joined: Sun Jun 18, 2006 8:48 pm

Post » Fri Jul 26, 2013 3:12 pm

You'll need to get input while inside the while loop (input should be verified as either yes/no since you don't want any other answers). You should have an array of questions rather than hard coding each one. Then increment through the array to retrieve the next question.

also, use code tags. This is extra-important when posting python code given how python is whitespace-sensitive.

Edit: Oh, also, what version of python are you using? Python before version 3 needs to use raw_input() to get strings.

User avatar
stacy hamilton
 
Posts: 3354
Joined: Fri Aug 25, 2006 10:03 am

Post » Fri Jul 26, 2013 11:31 am

One problem you're facing is using input instead of raw_input. Input takes a number, where raw_input takes a string.
mystring = raw_input("Write something: ")
This will then allow you to compare it to another string.
if mystring == "ten angry birds":	print "why'd you write that"
This outputs "why'd you write that" if you write "ten angry birds". One last issue in your code is an infinite loop. If you're going to use a while loop until a certain input is given, be sure to allow the user to enter a new input every iteration. Like so...
mystring = raw_input("Write something: ")while mystring != "ten angry birds":	print "this is not the answer we're looking for\n"	mystring = raw_input("Write something: ")
You can also write the above loop like this, but you won't be capable of comparing the input's value elsewhere.
while raw_input("Write something: ") != "ten angry birds":	print "this is not the answer we're looking for\n"
Happy coding. :D
User avatar
Mark Churchman
 
Posts: 3363
Joined: Sun Aug 05, 2007 5:58 am

Post » Fri Jul 26, 2013 6:17 am

It's time to upgrade to python 3.3

Note: That's my opinion, anyway. There used to be a big disparity between 2.7 and 3.x, but 3.3 performs well and most libraries and frameworks have made the port (well, of those still alive, anyway). As such I've stopped bothering with 2.7 and embraced the future.
User avatar
StunnaLiike FiiFii
 
Posts: 3373
Joined: Tue Oct 31, 2006 2:30 am

Post » Fri Jul 26, 2013 10:40 am

from programming logic

you trapped yourself inside an infinite loop if variable doesn't equal yes
User avatar
A Lo RIkIton'ton
 
Posts: 3404
Joined: Tue Aug 21, 2007 7:22 pm

Post » Fri Jul 26, 2013 3:58 pm

I remember the good, ol' days when I remembered how to quote somebody (and even remove what they said and just "tag" them in my new post, so-to-speak).

I am running Python 3.2. Started off on 2.7, but just upgraded and I am liking it more.

So I guess to start, I don't need raw_input

So if I am stuck inside an infinite loop, I should change my code to...

mystring = input("Write something: ")
while mystring != "ten angry birds":
print "this is not the answer we're looking for\n"
mystring = input("Write something: ")

You know, studying your example AP, it makes so much more sense!

Defron:

As someone who is actively/inactively using 3.3, what sort of suggestions would you offer to a newbie Python 3.x programmer?

Same question back to you Defron, but less 3.x answers obviously!

:smile: Thanks guys! I'm going to try tinkering with my code. I've practiced for hours today, and I just took my first break for a couple hours, so I'm ready to go again now. I definitely see how taking breaks is important.

Edit: Ditre, can you explain how I exactly trapped myself inside an infinite loop?

User avatar
Deon Knight
 
Posts: 3363
Joined: Thu Sep 13, 2007 1:44 am

Post » Fri Jul 26, 2013 9:02 am

So this is where I am encountering my first issue:

7. if xxxx != yes:

8. print("xxxxxx")

Traceback (most recent call last):
File "C:/Python33/xxxxx.py", line 7, in
if excited != yes:
NameError: name 'yes' is not defined
I need to define yes? I thought my string was needed to be defined, but I incorporated it earlier as:
5. xxxx= input()
The defined word on line 7 is 5 (what's the name of it again?).
What am I doing wrong?
User avatar
Agnieszka Bak
 
Posts: 3540
Joined: Fri Jun 16, 2006 4:15 pm

Post » Fri Jul 26, 2013 11:39 am

after your while loop begins there isn't any line that changes the variable or exits the loop

User avatar
brenden casey
 
Posts: 3400
Joined: Mon Sep 17, 2007 9:58 pm

Post » Fri Jul 26, 2013 5:30 am

yes is being seen as a variable. You need to put it in quotes
User avatar
Tiffany Carter
 
Posts: 3454
Joined: Wed Jul 19, 2006 4:05 am


Return to Othor Games