VERY BASIC HELP

  • Thread starter vignesh.harikrishna
  • Start date

V

vignesh.harikrishna

c=int(raw_input("How many numbers do you want to work? (Min. 2 Max. 3)"))
if c==2:
x=int(raw_input("Enter the first number to be worked"))
y=int(raw_input("Enter the second number to be worked"))
elif c==3:
x=int(raw_input("Enter the first number to be worked"))
y=int(raw_input("Enter the second number to be worked"))
z=int(raw_input("Enter the third number to be worked"))
else:
print "Invalid input.";raw_input("Press <enter> to close this window");exit
p=int(raw_input("Do you want to divide, subtract, add or multiply these numbers? (1=divide, 2=subtract, 3=add, 4=multiply)"))
if p==1 and c==2:
print "The result is : ";x/y
elif p==1 and c==3:
print "The result is :";x/y/z
elif p==2 and c==2:
print "The result is :";x-y
elif p==2 and c==3:
print "The result is :";x-y-z
elif p==3 and c==2:
print "The result is :";x+y
elif p==3 and c==3:
print "The result is :";x+y+z
elif p==4 and c==2:
print "The result is :";x*y
elif p==4 and c==3:
print "The result is :";x*y*z
else:
print "Invalid Input.";raw_input("Press <enter> to close this window")






That is my program. These are the problems I am having :

1. Even if c is not 2 or 3, the program continues, as if it received a valid input, it does not exit as I have tried to code it to.
2. If all values are entered correctly, the result does not display. It shows up as "The result is :" and just blank.

PLEASE HELP
 
Ad

Advertisements

C

Chris Angelico

print "Invalid input.";raw_input("Press <enter> to close this window");exit
1. Even if c is not 2 or 3, the program continues, as if it received a valid input, it does not exit as I have tried to code it to.

In Python, exit isn't a statement, it's a function. So you need to write:

exit()

to actually terminate.
elif p==3 and c==3:
print "The result is :";x+y+z
2. If all values are entered correctly, the result does not display. It shows up as "The result is :" and just blank.

The semicolon ends the print statement, and then you simply evaluate
and do nothing with the sum. Try a comma instead - that'll make it a
second argument to print, so it'll be printed out as you expect.

Thank you for making your problem so clear. You've given your code,
and you've said exactly what it's doing that you don't expect. I
really appreciate that! But one thing I would ask: Next time, please
consider your subject line. That's the first chance you have to grab
someone's attention - "VERY BASIC HELP" doesn't say _what_ you need
help with. :) Your post makes a nice change from some I've seen,
though...

Have fun, happy Pythoning!

ChrisA
 
J

John Gordon

In said:
c=int(raw_input("How many numbers do you want to work? (Min. 2 Max. 3)"))
if c==2:
x=int(raw_input("Enter the first number to be worked"))
y=int(raw_input("Enter the second number to be worked"))
elif c==3:
x=int(raw_input("Enter the first number to be worked"))
y=int(raw_input("Enter the second number to be worked"))
z=int(raw_input("Enter the third number to be worked"))
else:
print "Invalid input.";raw_input("Press <enter> to close this window");exit
p=int(raw_input("Do you want to divide, subtract, add or multiply these numbers? (1=divide, 2=subtract, 3=add, 4=multiply)"))
if p==1 and c==2:
print "The result is : ";x/y
elif p==1 and c==3:
print "The result is :";x/y/z
elif p==2 and c==2:
print "The result is :";x-y
elif p==2 and c==3:
print "The result is :";x-y-z
elif p==3 and c==2:
print "The result is :";x+y
elif p==3 and c==3:
print "The result is :";x+y+z
elif p==4 and c==2:
print "The result is :";x*y
elif p==4 and c==3:
print "The result is :";x*y*z
else:
print "Invalid Input.";raw_input("Press <enter> to close this window")
That is my program. These are the problems I am having :
1. Even if c is not 2 or 3, the program continues, as if it received a
valid input, it does not exit as I have tried to code it to.

That's because your code is this:

exit

instead of this:

exit()

In other words, you're referring to the exit function, but not actually
calling it.
2. If all values are entered correctly, the result does not display. It
shows up as "The result is :" and just blank.

That's because you're using a semicolon after the print statement.

This code is really two completely separate statements:

print "The result is : ";x/y

It prints the message and then, as a separate action, it calculates the
value of x/y (and then throws that value away, because it isn't assigned
anywhere.)

Use a comma instead of a semicolon, like this:

print "The result is : ", x/y
 
V

vignesh.harikrishna

Thank you both so much! I'll be sure to make more pertinent subject lines now :) Thanks for the detailed explanations! Clearly, I've just started learning this language ~20 minutes before I made this post, and am still learning the basics. Do you guys know of any guides for a beginner? I am definitely willing to take the time to learn in depth :)
 
J

Joel Goldstick

Thank you both so much! I'll be sure to make more pertinent subject lines
now :) Thanks for the detailed explanations! Clearly, I've just started
learning this language ~20 minutes before I made this post, and am still
learning the basics. Do you guys know of any guides for a beginner? I am
definitely willing to take the time to learn in depth :)

check out the documentation links on python.org to start
 
R

rusi

Thank you both so much! I'll be sure to make more pertinent subject linesnow :) Thanks for the detailed explanations! Clearly, I've just started learning this language ~20 minutes before I made this post, and am still learning the basics. Do you guys know of any guides for a beginner? I am definitely willing to take the time to learn in depth :)

Have you seen http://docs.python.org/2/tutorial/ ??
Its kind of required reading for beginners. A little time spent on that will save a lot on head-scratchers avoided.

After that there are the language and the library references
http://docs.python.org/2/reference/index.html#reference-index
http://docs.python.org/2/library/index.html#library-index

The library is ok if you stick to modules that make sense to you.
The language-ref is too heavy-going for a beginner -- other material/books may be preferable.
 
Ad

Advertisements

M

Mark Lawrence

Thank you both so much! I'll be sure to make more pertinent subject lines now :) Thanks for the detailed explanations! Clearly, I've just started learning this language ~20 minutes before I made this post, and am still learning the basics. Do you guys know of any guides for a beginner? I am definitely willing to take the time to learn in depth :)

As a newbie you might like to try the tutor mailing list see
https://mail.python.org/mailman/listinfo/tutor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top