VERY BASIC HELP
John Gordon
gordon at panix.com
Mon Sep 30 13:25:46 EDT 2013
In <380132bc-bc9c-4d57-95d8-dc01f26f47a5 at googlegroups.com> vignesh.harikrishna at gmail.com writes:
> 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
--
John Gordon A is for Amy, who fell down the stairs
gordon at panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
More information about the Python-list
mailing list