1

I am new to Python and I have one question about the same topic. I have this simple calculator but the thing is I want all inputs to be on a single line. For example to have print "Enter Numbers" and to be able to enter in the same line like num1-operator-num2 (5+2) and on the next line to have the print "The result is:" Can anyone help with this one? :D

num1 = float(input("Enter First Number:"))
op = input("Enter operator: ")
num2 = float(input("Enter Second Number:"))

if op == "+":
    print(num1 + num2)
elif op == "-":
    print(num1 - num2)
elif op == "*":
    print(num1 * num2)
elif op == "/":
    print(num1 / num2)
ajss
  • 13
  • 2
  • Does this answer your question? [Python: "Print" and "Input" in one line](https://stackoverflow.com/questions/30142107/python-print-and-input-in-one-line) – adamgy Jun 02 '20 at 23:09

1 Answers1

0

Here. You just need to add two arguments to the print function. First is the string, then the actual answer.

Code:

exp = input("Enter the expression you want to evaluate: ")

print("The answer is: ",eval(exp))

Hope this helps!

10 Rep
  • 2,217
  • 7
  • 19
  • 33
  • Thanks. But how can I make the inputs to be in same line since they are different data type? I want to enter for example: 5+2 and then the result to show on the next line – ajss Jun 02 '20 at 23:14
  • This does not solve the problem, which was to show the inputs on the same line. – adamgy Jun 02 '20 at 23:15
  • @ajss Wait, what do you mean? The result shows on the next line by default. – 10 Rep Jun 02 '20 at 23:17
  • Yes, but each input is going on a different line. I want to skip the part when i need to enter first numbet: 5 #and then press enter to go on the operator and then again enter to go to the second number. i just want to enter the numbers and the operator on a single line like: Input: 5+2 Print: The Result Is: 7 – ajss Jun 02 '20 at 23:20
  • @ajss I edited my answer, see if the code works for you. – 10 Rep Jun 02 '20 at 23:26
  • @ajss I am not familiar with C. I assume there would be a similar function in C, but check [this](https://stackoverflow.com/a/39105000/12708583) out. – 10 Rep Jun 03 '20 at 00:02
  • thanks man, it works just great. and making it with just 2 lines of code makes my code to look stupid :D but is it possible to make my code to work that way? everything to show in one line? – ajss Jun 03 '20 at 00:05
  • @ajss You can do this one liner: `exp = input("Enter the expression you want to evaluate: "); print("The answer is: ",eval(exp))`. However, this is bad practice, and I wouldn't recommend it. – 10 Rep Jun 03 '20 at 00:22
  • @ajss Also, would you mind upvoting and accepting my answer? I would really appreciate it! – 10 Rep Jun 03 '20 at 00:23
  • how can we make my code to work the same way as yours? i want to learn how to make inputs and prints to go in a single line since every print and input goes in a new line – ajss Jun 03 '20 at 00:30
  • @ajss That isn't possible. The input statement always goes on a new line. – 10 Rep Jun 04 '20 at 21:09