Need help with my Python code (Multiplication) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need help with my Python code (Multiplication) (/thread-35148.html) |
Need help with my Python code (Multiplication) - NeedHelpPython - Oct-03-2021 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. RE: Need help with my Python code (Multiplication) - Yoriz - Oct-03-2021 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 whitespaceuser_input = "7 * 7" first_number, symbol, second_number = user_input.split() RE: Need help with my Python code (Multiplication) - Pedroski55 - Oct-04-2021 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! |