eight.coffee.beans
December 6th, 2014, 04:10 AM
So, I started using Python because we've started coding in it at university and now we have second homework to do.
I did the bigger piece of the code, however I'm stuck in making a condition.
The full task is this:
Create a calculator which will add up, multiply, divide or subtract the two numbers that user enters.
When calculator finishes the process it must ask user if he/she wants to repeat the program, also if user wants to divide the number by 0 make the program give error message.
Here's what I've got:
#LINE TO REPEAT THE PROGRAM
restart = "Y"
while restart == "Y":
#PRINTING OUT THE MAIN MENU
print("Select one option please:")
print("----------------------------")
print("1 for adding up")
print("2 for subtracting")
print("3 for multiplying")
print("4 for dividing")
print("5 for exponenting")
print("---------------------------")
#REQUEST FOR USER TO CHOOSE
choice_raw = input("Your choice is? \n")
#CONVERTING INPUT INTO INTEGER
choice = int(choice_raw)
#IF INPUT IS GREATER THAN 5 ERROR APPEARS
if choice > 5:
print("No function found for the number:", choice)
#IF NUMBER ONE IS CHOSEN
elif choice == 1:
print("Adding up")
first_number = input("Enter first number: \n")
second_number = input("Enter second number: \n")
result_one = int(first_number) + int(second_number)
print("Rezultat:", result_one)
#IF NUMBER TWO IS CHOSEN
elif choice == 2:
print("Subtracting")
first_number = input("Enter first number: \n")
second_number = input("Enter second number: \n")
result_two = int(first_number) - int(second_number)
print("Rezultat:", result_two)
#IF NUMBER THREE IS CHOSEN
elif choice == 3:
print("Multiplying")
first_number = input("Enter first number: \n")
second_number = input("Enter second number: \n")
result_three = int(first_number) * int(second_number)
print("Rezultat:", result_three)
#IF NUMBER FOUR IS CHOSEN
elif choice == 4:
print("Dividing")
first_number = input("Enter first number: \n")
second_number = input("Enter second number: \n")
result_four = float(first_number) / float(second_number)
print("Rezultat:", result_four)
#IF NUMBER FIVE IS CHOSEN
else:
print("Odabrali ste potenciranje")
first_number = input("Enter base number: \n")
second_number = input("Enter exponent number: \n")
result_five = int(first_number) ** int(second_number)
print("Rezultat:", result_five)
#ASKING USER TO RESTART THE CODE
restart = input("Restar (Y/n)? \n")
I've tried using if or elif inside if, but I got some errors.
Please remember that this has to be done in 'newbie' way and please if possible explain what you did.
In advance thanks to all.
I did the bigger piece of the code, however I'm stuck in making a condition.
The full task is this:
Create a calculator which will add up, multiply, divide or subtract the two numbers that user enters.
When calculator finishes the process it must ask user if he/she wants to repeat the program, also if user wants to divide the number by 0 make the program give error message.
Here's what I've got:
#LINE TO REPEAT THE PROGRAM
restart = "Y"
while restart == "Y":
#PRINTING OUT THE MAIN MENU
print("Select one option please:")
print("----------------------------")
print("1 for adding up")
print("2 for subtracting")
print("3 for multiplying")
print("4 for dividing")
print("5 for exponenting")
print("---------------------------")
#REQUEST FOR USER TO CHOOSE
choice_raw = input("Your choice is? \n")
#CONVERTING INPUT INTO INTEGER
choice = int(choice_raw)
#IF INPUT IS GREATER THAN 5 ERROR APPEARS
if choice > 5:
print("No function found for the number:", choice)
#IF NUMBER ONE IS CHOSEN
elif choice == 1:
print("Adding up")
first_number = input("Enter first number: \n")
second_number = input("Enter second number: \n")
result_one = int(first_number) + int(second_number)
print("Rezultat:", result_one)
#IF NUMBER TWO IS CHOSEN
elif choice == 2:
print("Subtracting")
first_number = input("Enter first number: \n")
second_number = input("Enter second number: \n")
result_two = int(first_number) - int(second_number)
print("Rezultat:", result_two)
#IF NUMBER THREE IS CHOSEN
elif choice == 3:
print("Multiplying")
first_number = input("Enter first number: \n")
second_number = input("Enter second number: \n")
result_three = int(first_number) * int(second_number)
print("Rezultat:", result_three)
#IF NUMBER FOUR IS CHOSEN
elif choice == 4:
print("Dividing")
first_number = input("Enter first number: \n")
second_number = input("Enter second number: \n")
result_four = float(first_number) / float(second_number)
print("Rezultat:", result_four)
#IF NUMBER FIVE IS CHOSEN
else:
print("Odabrali ste potenciranje")
first_number = input("Enter base number: \n")
second_number = input("Enter exponent number: \n")
result_five = int(first_number) ** int(second_number)
print("Rezultat:", result_five)
#ASKING USER TO RESTART THE CODE
restart = input("Restar (Y/n)? \n")
I've tried using if or elif inside if, but I got some errors.
Please remember that this has to be done in 'newbie' way and please if possible explain what you did.
In advance thanks to all.