-2

I've been trying to repeat my code back to the first line in Python. I was making a simple calculator and once I run it, it works just fine when the process is over I can't use it again. I need to run it again to calculate again.

This is the code. I wanted it to go back to the start so that I don't have to run it again.

#Values

Question = input("Would you like to use our Simple Calculator Y/N :  ")

#If Yes

if Question == "Y":
    print("Sure")
    
    #if Question == "N":
    #print("Ok")


first_number = input("What is your first number:")
second_number = input("What is your second number:")
function = input("What calculation would you like perormed:")

first_number = int(first_number)
second_number = int(second_number)

if function == "+":
    print(first_number+second_number)
    
if function == "-":
    print(first_number-second_number)
    
if function == "*":
    print(first_number*second_number)
    
if function == "/":
    print(first_number/second_number)    
    
if Question == "N":
    print("Ok")
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
prime
  • 1
  • This duplicate closure is temporary - I think the linked duplicate is a separate question that matches this one, and not itself a duplicate (although it is linked as one). I argued for this in the [Python chat room](https://chat.stackoverflow.com/rooms/6/python) but I won't be around to see responses for a while. – Karl Knechtel Aug 13 '22 at 08:02

3 Answers3

2

use while:

while your_condition_here:
    # your code here will repeat while condition is True

You can do is ask the user if they want to continue and use their response in your condition.

Kache
  • 15,647
  • 12
  • 51
  • 79
0
Question = input("Would you like to use our Simple Calculator Y/N :  ")

#If Yes
if Question == "Y":
    print("Sure")

    while(1):
        first_number = input("What is your first number:")
        second_number = input("What is your second number:")
        function = input("What calculation would you like performed:")

        first_number = int(first_number)
        second_number = int(second_number)

        if function == "+":
            print(first_number+second_number)

        if function == "-":
            print(first_number-second_number)

        if function == "*":
            print(first_number*second_number)

        if function == "/":
            print(first_number/second_number)    

if Question == "N":
    print("Ok")
imranjust14
  • 97
  • 1
  • 7
0
while True:
    Question = input("Would you like to use our Simple Calculator Y/N :  ")
    if Question == "Y":
        print("Sure")

        first_number = input("What is your first number:")
        second_number = input("What is your second number:")
        function = input("What calculation would you like perormed:")

        first_number = int(first_number)
        second_number = int(second_number)

        if function == "+":
            print(first_number+second_number)

        if function == "-":
            print(first_number-second_number)

        if function == "*":
            print(first_number*second_number)

        if function == "/":
            print(first_number/second_number)    

    elif Question == "N":
        
        print("Ok")
        break
Mehmaam
  • 573
  • 7
  • 22