Python Forum
multiplication by successive addition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiplication by successive addition
#1
Guys, im from Brazil and I just started a python course a couple days back. This program is supposed to multiply 2 numbers and show the subtotal of the calculation. However, in this project, i'm not supposed to use the "*" operator, just "-" or "+". How can I do that? In my code, I used "*" twice, as you can see.
Thanks.

def back():
    while True:
        try:
            m1=input("Number 1: ")
            m2=input("Number 2: ")
            m1=int(m1)
            m2=int(m2)
            result=m1*m2 #### i'm not supposed to use "*"
            save1=m1
            save2=m2
        except ValueError:
            print("Error in number 1 or 2. Try again.")
            continue
        break
    subtotal=0
    if m1<m2:
        aux = m2
        m2 = m1
        m1 = aux
    if m2>0:
        while m2>0:
            m2=m2-1
            subtotal=subtotal+m1
            print("ST:", subtotal)
    if m2<0:
        while m2<0:
            m2=m2+1
            subtotal=subtotal+m1
            print("ST:", subtotal * -1)   ### again, i'm not supposed to use "*"
    print(save1,"*",save2,"=", result)
    reset=input("Type Y for a new operation")
    if reset == "Y" or reset == "y" or reset == "Yes" or reset == "yes":
        back()
    else:
        print("Closing...")
back()
Reply
#2
If you need to multiply x and y using addition, you add up y x's. 2 * 3 = 2 + 2 + 2. So use a for loop. Start with the result equal to zero. Have a for loop to loop y times. Each time through the loop add x to the result.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiply and Addition in the same loop statement with logic. joelraj 2 1,029 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  Multiplication Table code alexsendlegames100 3 1,359 Jun-06-2022, 09:45 AM
Last Post: Gribouillis
  forloop to compute sum by alternating from addition to subtraction JulianZ 3 1,800 Apr-02-2022, 09:36 AM
Last Post: DeaD_EyE
  Try to solve GTG multiplication table problem. Frankduc 6 1,979 Jan-18-2022, 08:26 PM
Last Post: Frankduc
  Need help with my Python code (Multiplication) NeedHelpPython 2 1,676 Oct-04-2021, 12:09 PM
Last Post: Pedroski55
  Order a list with successive permutations based on another list yvrob 3 2,787 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  List conversion and multiplication johnkyp 5 3,152 Jan-02-2020, 08:20 AM
Last Post: perfringo
  Matrix Multiplication Issue VIJENDRA 1 1,855 Dec-19-2019, 06:16 PM
Last Post: Gribouillis
  Multiplication between a list and a variable doug2019 2 2,152 Oct-08-2019, 04:10 AM
Last Post: doug2019
  Multiplication Table number margins CJ707 4 2,413 Sep-18-2019, 02:16 PM
Last Post: CJ707

Forum Jump:

User Panel Messages

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