1

So I have completed a python program which can essentially make a calculator work in real life, i.e. my code will do this:

>>>3*6
>>>18

So, this works very much like a real calculator EXCEPT for the fact that a calculator would print it out on the same line (talking about basic calculator). for example:

>>>3*6= 18

3*6 is input and = 18 is printed by program, and I don't want them in different line. So, if anyone could help me make it on the same line by giving me ideas, it would be appreciated so my calculator would look more pretty. Also, please let me know if my code can be prettier, thanks ;).

OK, here is the pretty messy code:

def addition(a,b):#this will do addition
    print (a+b)

def  subtraction(a,b):
    print(a-b)

def mutiplication(a,b):
    print(a*b)

def division(a,b):
    print (a/b)

def modulo(a,b):
    print(a%b)



def mainoperations(a,b,number, expression):#this will be the place       where you will be doing calculations
    print("basic calculator: use four operations, +, -, /, *")

    indexn=0#the index number starts at 0
    while(indexn<len (expression)):#indexn is the index number of the operation you have typed.
            if(indexn != '1' or '2' or '3' or '4' or '5' or '6' or '7'or  '8' or '9' or '0'):
                    indexn+=1#this will look at every index of the expression


                if(indexn=="+"):#if an addition sign is seen...
                        addition(expression[:indexn-1],expression[indexn+1:])

                elif(indexn=="-"):#if a subtraction sign is seen...
                        subtraction(expression[:indexn-1],expression[indexn+1:])

                elif(indexn=="*"):#if a multiplication sign is seen...
                        multiplication(expression[:indexn-1],expression[indexn+1:])

                elif(indexn=="/"):#if a division sign is seen...
                        division(expression[:indexn-1],expression[indexn+1:])

                elif(indexn=="%"):#if a modulo sign seen...
                        modulo(expression[:indexn-1],expression[indexn+1:])


expression=input()#the expression is what you inputed
mainoperations#do the function
learner
  • 3,168
  • 3
  • 18
  • 35
Darkrai33
  • 51
  • 7
  • 2
    i can't answer,but I can help a bit: python's way to detect when a ```input()```end is using a ```\n```, which means new line, so the ```input()```only end when you type in ```enter```, so you can't prevent new line,but you can find a way to delete```\n```, which cancel new line. – okie Nov 13 '19 at 04:03
  • [Possible duplicate](https://stackoverflow.com/questions/7173850/possible-to-get-user-input-without-inserting-a-new-line) – Clément Nov 13 '19 at 09:00

1 Answers1

1

In print() statement, there is a parameter names 'end'. You can use it to tell python interpreter not to shift the content to the next line. It's default value is '\n' which indicates new line.

For example-

print('Hello', end=' ')
print('World!')

Output-

Hello World!

You can read more about print() function and it's parameters from here.

If you have any doubt, feel free to ask in comments. :)

Chetan Goyal
  • 435
  • 5
  • 14