Python programming help? im a noob.

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

Python programming help? im a noob.

Post by EpiC BolT on Thu Nov 24, 2011 2:48 pm
([msg=63042]see Python programming help? im a noob.[/msg])

So I've just started python and to get a little better I decided to make a simple program that asks you questions and you can talk back to it. basically this is the only code I have so far (im a noob lol)

x=raw_input ('Enter Age Here: ')
print "That is nice, I am also " + x
y=raw_input ('where were you born?: ')
print "Thats pretty cool, How is it in " + y "I was born in Paraguay its really nice there"
raw_input ("press <enter> to end")

so every time I run this program to see if it works its telling me that i have an error and it is highlighting this part (i wull underline.
print "Thats pretty cool, How is it in " + y "I was born in Paraguay its really nice there"

what I want to make is that after you answer the question of where you were born the program says "thats pretty cool, how is it in (and then it says the place the person answered)" and then the program says it was born in Paraguay, but it doesnt seem to work. helppp pleaseeee.
basically i want it like this. (computer talking - user input - computer talking) in one sentence.

-- Thu Nov 24, 2011 2:55 pm --

I did notice that if i type the program like this it does do what i want it to
x=raw_input ('Enter Age Here: ')
print "That is nice, I am also " + x
y=raw_input ('where were you born?: ')
print "Thats pretty cool, How is it in " + y
print 'I was born in Paraguay its really nice there'
raw_input ("press <enter> to end")

but if i do that i have to put an extra print line under the "thats pretty cool,..." line and I would like it do all go on the same line.

-- Thu Nov 24, 2011 3:00 pm --

Also, sorry i keep posting stuff lol, lets say i ask a yes or no question in the program, is there a way to type code that would make the program act differently incase the use types yes or no.? I know theres a way to do this in Visual Basic .NET but I have no idea in python.
EpiC BolT
New User
New User
 
Posts: 14
Joined: Mon Nov 21, 2011 7:06 pm
Blog: View Blog (0)


Re: Python programming help? im a noob.

Post by jgreen45 on Fri Nov 25, 2011 12:32 pm
([msg=63045]see Re: Python programming help? im a noob.[/msg])

EpiC BolT wrote:So I've just started python and to get a little better I decided to make a simple program that asks you questions and you can talk back to it. basically this is the only code I have so far (im a noob lol) <br><br>x=raw_input ('Enter Age Here: ')<br>print "That is nice, I am also " + x<br>y=raw_input ('where were you born?: ')<br>print "Thats pretty cool, How is it in " + y "I was born in Paraguay its really nice there"<br>raw_input ("press <enter> to end")<br><br>so every time I run this program to see if it works its telling me that i have an error and it is highlighting this part (i wull underline.<br>print "Thats pretty cool, How is it in " + y "I was born in Paraguay its really nice there"<br><br>what I want to make is that after you answer the question of where you were born the program says "thats pretty cool, how is it in (and then it says the place the person answered)" and then the program says it was born in Paraguay, but it doesnt seem to work. helppp pleaseeee. <br>basically i want it like this. (computer talking - user input - computer talking) in one sentence.<br><br>-- Thu Nov 24, 2011 2:55 pm --<br><br>I did notice that if i type the program like this it does do what i want it to <br>x=raw_input ('Enter Age Here: ')<br>print "That is nice, I am also " + x<br>y=raw_input ('where were you born?: ')<br>print "Thats pretty cool, How is it in " + y<br>print 'I was born in Paraguay its really nice there'<br>raw_input ("press <enter> to end")<br><br>but if i do that i have to put an extra print line under the "thats pretty cool,..." line and I would like it do all go on the same line.<br><br>-- Thu Nov 24, 2011 3:00 pm --<br><br>Also, sorry i keep posting stuff lol, lets say i ask a yes or no question in the program, is there a way to type code that would make the program act differently incase the use types yes or no.? I know theres a way to do this in Visual Basic .NET but I have no idea in python.


As far as trying to get print "Thats pretty cool, How is it in " + y "I was born in Paraguay its really nice there" working on one line. You need another + in there, like this:

print "Thats pretty cool, How is it in " + y + "I was born in Paraguay its really nice there"

For your next part you can always use an if statement, to check if the user inputed "yes" or "no". Something like:

if [input_variable_goes_here] == "yes":
[Do whatever here]
elif [input_variable_goes_here] == "no":
[blah]
else:
print("N00B")

It's always nice to add an extra else statementment in case of random shizz going down.
I can't come to bed...
Someone is WRONG on the internet


http://xkcd.com/386/
User avatar
jgreen45
Poster
Poster
 
Posts: 106
Joined: Wed Feb 25, 2009 6:18 pm
Blog: View Blog (0)


Re: Python programming help? im a noob.

Post by EpiC BolT on Fri Nov 25, 2011 2:03 pm
([msg=63046]see Re: Python programming help? im a noob.[/msg])

thanks Ill put that in :D

one more thing..

when asking for the persons age I want it to know if the person types an actual number or a letter, and to put different replies for each case. so this is what i type

x=raw_input ('Enter Age Here: ')
if x <5 or x >100:
print 'cool I am 17'
else:
print 'are you lying?'

but it is completly ignoring the first if statement and its going straight to the else. what am I doing wrong??
EpiC BolT
New User
New User
 
Posts: 14
Joined: Mon Nov 21, 2011 7:06 pm
Blog: View Blog (0)


Re: Python programming help? im a noob.

Post by Defience on Fri Nov 25, 2011 4:04 pm
([msg=63050]see Re: Python programming help? im a noob.[/msg])

There are different ways of doing this. Try this:

Code: Select all
x=input('Enter Age Here: ')  # use 'input' for integers, use 'raw_input' when saving strings.
if x==17:                  # If x equals 17 print statement.
    print "That is nice, I am also %i" % x  #use %i to substitute with x variable (integer)
if x!=17:                       # != means 'not equal to' so if x does not equal 17 print statement.
    print "That's pretty cool!"
y=raw_input ('where were you born?: ')

# %s to substitute string for y variable
print "Thats pretty cool, How is it in %s, I was born in Paraguay its really nice there" % y
raw_input ("press <enter> to end")


Second:
Code: Select all
x=input ('Enter Age Here: ') # use 'input' for integers
if x <10:                            # if x is less than 10, print
   print 'You are young!'
elif x >=10:                        # if x is greater than or equal to 10, print.
   print 'are you lying?'
User avatar
Defience
Addict
Addict
 
Posts: 1281
Joined: Thu Jun 12, 2008 3:16 pm
Blog: View Blog (0)


Re: Python programming help? im a noob.

Post by eggscrambler on Thu Dec 01, 2011 1:08 am
([msg=63144]see Re: Python programming help? im a noob.[/msg])

it seems to me that you are using python 3.x This means that there is only one way to make it take only numbers. If you are using python 2.x however you can use this same method which is better than using input().
Code: Select all
x= int(raw_input("Enter your age here: ")
#Note if the do not enter a number an error will be made you must use try and throw to avoid this or put in other lines of code
# Also if you would like a float number like 10.0 instead of 10 you can do float(raw_input())
eggscrambler
New User
New User
 
Posts: 16
Joined: Thu Apr 28, 2011 11:30 pm
Blog: View Blog (0)


Re: Python programming help? im a noob.

Post by Defience on Thu Dec 01, 2011 4:40 pm
([msg=63146]see Re: Python programming help? im a noob.[/msg])

I doubt that he's using 3.x as his print statements would have to be print () functions.
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

cron