Python Forum
Need help with my Python code (Multiplication)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with my Python code (Multiplication)
#1
print("\n----Task 4---- Multiple numbers")

numbers = input("Input numbers to multiply: ")
num_1 = int()
num_2 = int()
print("Extracted numbers " + str(num_1) + " " + str(num_2))
print(str(num_1)  + " * " + str(num_2) + " = " + str(num_1*num_2))
Basically one of my tasks is to make a multiplication calculator so that when the user inputs lets say 7 * 7 it gives them 49. However I am stumped on how to do this.

I can do one like this
A = int(input('First number: '))

B = int(input('Second number: '))

print(A * B)
But I want it to be in a single input. Not multiple inputs.
Yoriz write Oct-03-2021, 07:58 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You can use the string method split to return a list of the words in the string, using sep as the delimiter string.
sep defaults to whitespace
user_input = "7 * 7"
first_number, symbol, second_number = user_input.split()
Reply
#3
Good practice making a calculator!

What if you want more than 2 numbers?

A while loop is useful. Also, you need to deal with the operand.

I did this when I began to learn some python. (Still haven't got very far!)

#! /usr/bin/python3

# a function to get the numbers
def getNums():
    numbers = []
    while True:
        print('Positive numbers don\'t need a + symbol in front.') 
        print('If you want a negative number, just enter -4 or -15, like that.')
        print('Decimal numbers as normal: 1.35, 12.78 etc.\n\n')
        num = input('Enter a number, or q to quit/stop entering numbers ... ')
        if num == 'q':
            break
        numbers.append(float(num))
    return numbers

# a function to get the operand
def getOperand():
    # maybe add some other operands
    operands = ['+', '-', '*', '/']
    operand = 'X'
    while not operand in operands:
        print('\n\nPlease choose + - * or / ')
        operand = input('Enter what you want to do" + - * / ... ')
    return operand

# a function to add the numbers
def add(numbs):
    result = 0
    for num in numbs:
        result = result + num
    return result

# you make the other functions
# a function to multiply the numbers
def multiply(numbs):
    result = 1
    print('len numbers is', len(numbs))
    for i in range(len(numbs)):
        print('i is', i)
        # do something here
        #
        print('result =', result)
    return result

def main():
    while True:
        message = input('\n\n Enter q to quit using the calculator, anything else to keep calculating ... ')
        if message == 'q':
            print('再见, see you again!')
            break
        the_nums = getNums()
        operand = getOperand()

        if operand == '+':
            this_result = add(the_nums)
            print('\n\nResult  of addition is', this_result)
            
        # you can figure out the other functions for - * and /
        if operand == '-':
            this_result = subtract(the_nums)
            print('\n\nResult of subtraction is', this_result)            

        if operand == '*':
            this_result = multiply(the_nums)
            print('\n\nResult of multiplication is', this_result)
            
        if operand == '/':
            this_result = multiply(the_nums)
            print('\n\nResult of division is', this_result)
            
if __name__ == "__main__":
    main()
Actually, you only need 2 functions, 1 for addition and 1 for subtraction. Those are really the only 2 operations in arithmetic.

Internally, how computers handle numbers is quite complicated. Somewhere, I have a series of videos "Advanced Python Everything is Bits and Bytes"

Too complicated for me! Have fun!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiplication Table code alexsendlegames100 3 1,317 Jun-06-2022, 09:45 AM
Last Post: Gribouillis
  Try to solve GTG multiplication table problem. Frankduc 6 1,937 Jan-18-2022, 08:26 PM
Last Post: Frankduc
  List conversion and multiplication johnkyp 5 3,104 Jan-02-2020, 08:20 AM
Last Post: perfringo
  Matrix Multiplication Issue VIJENDRA 1 1,831 Dec-19-2019, 06:16 PM
Last Post: Gribouillis
  Multiplication between a list and a variable doug2019 2 2,123 Oct-08-2019, 04:10 AM
Last Post: doug2019
  Multiplication Table number margins CJ707 4 2,365 Sep-18-2019, 02:16 PM
Last Post: CJ707
  multiplication by successive addition Zebrol 1 3,471 Sep-14-2019, 05:37 PM
Last Post: ichabod801
  Tracing a multiplication table w/ Python trace() NationalRex22 0 1,728 Jun-11-2019, 03:31 AM
Last Post: NationalRex22
  float multiplication - unexpected output inesk 3 3,257 Dec-11-2018, 10:59 AM
Last Post: DeaD_EyE
  Print every multiplication of recursion Arontbt 4 3,678 Apr-24-2018, 10:01 PM
Last Post: Arontbt

Forum Jump:

User Panel Messages

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