Whenever I go to subtract using more than 2 numbers it only does the first 2 and doesn’t use the final number. How do I fix it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Whenever I go to subtract using more than 2 numbers it only does the first 2 and doesn’t use the final number. How do I fix it?

My code is something I copy/pasted here since I’m looking for help https://code.sololearn.com/cpTF1bMet8vN/?ref=app

15th Dec 2020, 3:37 PM
Gilgames h
Gilgames h - avatar
4 Answers
+ 2
Gilgames h , this is a nice try from you for a calculator. but there is an issue with your conditions in if and elif clause. Just to give you a hint how you can solve that: You have to use a parenthesis to group the statement. Do this in all 3 parts of the statement. .... if amount==2 and (math=="subtract" or "SUBTRACT" or "Subtract"): subtract=list[0] - list[1]#subtracts 2 numbers print(subtract) .... In general you should try to reduce some duplicating code. Here are some hints: - try to use a shorter input instead of "subtract". Nobody will like to input this long word. Use "sub" or better "-" - when you are keeping input as a word, then convert it to lower, and check against a lower word: if amount==2 and math.lower() =="sub": - for the calculation you should not use a hard-coded 2,3 or 4 number case. Use a for loop to do the calculation for all members in the list, you don't have to care about how many members are in the list. happy coding!
15th Dec 2020, 4:41 PM
Lothar
Lothar - avatar
+ 2
here is my way to do calculation using eval() : WARNING : it is harmful to use eval() on input() because when you use modules such as os, subprocess in your program user can use commands such as "rm - rf *" it will delete all files and directories.. this is safe code 👇😊 : https://code.sololearn.com/c16vh5ryLL3N/?ref=app
15th Dec 2020, 4:44 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
As I see you wrote ' if amount == 2 and math == "subtract" or "Subtract" or "SUBTRACT" ' It should be ' if (amount == 2) and (math == "subtract" or math == "Subtract" or math == "SUBTRACT") ' But, you can simply do ' if amount == 2 and math.lower() == "subtract" ' I don't if this will solve your problem but it will make it better
15th Dec 2020, 4:27 PM
Sousou
Sousou - avatar
+ 1
Gilgames h you are using list (reserved keyword) as your variable name.. don't use it.. learn the basic of python) you write your condition like 👇 this : if amount==2 and math=="subtract" or "SUBTRACT" or "Subtract": actually this is not good practice in python simply you can use str.lower() to convert the user input in lowercase and then use it in your if condition. Example : math=input().lower() #SubtRaCT if math=="subtract": print("subtracting") #output : subtracting str.lower() function takes every character in a string and converts to its lowercase equivalent. I am unable to figure it out why your code is not working... 🤔 Here is your working code : https://code.sololearn.com/cyhGSC7K0fWx/?ref=app
15th Dec 2020, 4:34 PM
Ratnapal Shende
Ratnapal Shende - avatar