Challenge for beginners

+1 Vaibhav Patel · November 14, 2014
Hai all,
I am fairly new to the world of programming.
after 2 days of hard work, I created the below code. But I want to add a condition to it.

if user input string value as "X" or "Y", the system should throw message saying " Please insert numerical value",
if not than the system should produce the result.



-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
x = input(int)
y = input(int)

z = (int(x)+int(y))

print ("the sum of ",x,   "and ", y,  "is", z)

z = (int(x)-int(y))

print ("the difference of ", x , "and" , y , "is ", z)

z = (int(x)*int(y))

print ("the multiplication of " , x , "and", y, "is " , z)

z = (int(x)/int(y))

print ("the division of " , x , "and" , y, "is ", z)

z = (int(x)%int(y))

print ("the remainder of ", x ,"and", y , "is ", z)

Post a Reply

Replies

Oldest  Newest  Rating
+1 Doug Fresh · November 21, 2014
Well, you'll want to use a loop in order to check values. Specifically a try/except loop. It will try to complete the actions inside of a loop, they cannot be completed, then the program will display a message, as long as the exception isn't raised, the while loop will break and go on to the next. 


while True:
    ## we use a defensive approach so that our values will always return something when we want
    try:
        ## note the use of int(input to specify the input expected must be an integer.
        ## without the try/except loop, python would throw an error, and the program would break
        
        x = int(input("Please enter a numerical value for x: "))
    except ValueError:
        print("Please enter a number, sucka!")
    else:
        break
while True:
    
    try:
        y = int(input("Please enter a numerical value for y: "))
    except ValueError:
        print("Enter a dang number only. jeez")
    else:
        break


some = x+y
difference = x-y
multiply = x*y
## If y = 0,  you will get an error.
if y == 0:
    division = "Error, can't divide by 0"
    remainder =  "Error, can't divide by 0"
else:
    division = x/y
    remainder = x%y


print("The sum of {0} and {1}, is {2}.".format(x,y,some))
print("The difference of {0} and {1}, is {2}.".format(x,y,difference))
print("The multiplication of {0} and {1}, is {2}.".format(x,y,multiply))
print("The division of {0} and {1}, is {2}.".format(x,y,division))
print("The remainder of {0} and {1}, is {2}.".format(x,y,remainder))


except is used with any conditional statements, or Boolean values. 
+1 Vaibhav Patel · November 14, 2014
Sound a little smaller way to generate same result for the above code.

x = input(int)
y = input(int)

print ("the sum of ",x,   "and ", y,  "is", (int(x)+int(y)))
print ("the difference of ", x , "and" , y , "is ", (int(x)-int(y)))
print ("the multiplication of " , x , "and", y, "is " ,(int(x)*int(y)))
print ("the division of " , x , "and" , y, "is ", (int(x)/int(y)))
print ("the remainder of ", x ,"and", y , "is ",(int(x)%int(y)))
  • 1

Python

107,325 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator