Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do i repeat???
#1
i have some coding that i would like to repeat here is the coding how do i do it??? it is for a calculator and i want to repeat the last bit ("main()")
#returns the result of adding num1 + num2
def add(num1, num2):
    return num1 + num2

#returns the result of subtracting num1 - num2
def minus(num1, num2):
    return num1 - num2

#returns the result of mutipliying num1 * num2
def multiply(num1, num2):
    return num1 * num2

#returns the result of dividing num1 / num2
def divide(num1, num2):
    return num1 / num2


def main():
    operation = input("what do you want to do (+,-,*,/): ")
    if(operation != '+' and operation != '-' and operation != '*' and operation != '/'):
        #invalid operation
        print("please enter a vaild operation")
    else:
        num1 = int(input("Enter num1: "))
        num2 = int(input("Enter num2: "))
        if(operation == '+'):
            print(add(num1, num2))
        elif(operation == '-'):
            print(minus(num1, num2))
        elif(operation == '*'):
            print(multiply(num1, num2))
        else:
            print(divide(num1, num2))
            
main()
Reply
#2
Hello and welcome to the forum!
I see you are already familiar with if/elif/else statements. Next very useful thing to learn can be loops, and there you will also find (at least one) solution for your challenge. Good luck!
Reply
#3
See if you can pick up some hint in my post here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,258 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Repeat request by else stsxbel 2 1,150 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  get out of while loop and stop repeat Frankduc 11 2,866 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  Avoid multiple repeat in indent Frankduc 8 2,790 Jan-18-2022, 05:46 PM
Last Post: Frankduc
  How to discard list repeat values akanowhere 7 3,576 Dec-28-2020, 09:14 PM
Last Post: akanowhere
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,722 Nov-23-2020, 11:01 PM
Last Post: perfringo
  is there a way: repeat key word args Skaperen 2 2,200 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Python Script to repeat Photoshop action in folders and subfolders silfer 2 4,506 Jul-25-2019, 03:12 PM
Last Post: silfer
  Trying to Repeat a Function? DanielHetherington 1 2,310 Mar-27-2019, 09:48 PM
Last Post: SheeppOSU
  While loop repeat Runner83 5 4,200 Nov-11-2018, 10:50 AM
Last Post: MasterJediKnight7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020