-1

Is there anyway to print between two print commands? For example I print sin( and ) and between these parenthesis input amount of x for some function like sin(x). Like this:

print("sin("
x=float(input())
print(")")
>>sin("""input""")

I need to print sin(" then get input and print the value of x then print ). To make clear I want to use print and input parallel.

hamidfzm
  • 4,595
  • 8
  • 48
  • 80
  • 3
    show the desired output/user experience. this is still unclear as written. do you want to print `sin("` then get input and print the value of `x` then print `)`? Or you want to print the `sin` of `x`? The user has to hit return to get `input` to move on to the rest of the script. – beroe Nov 16 '13 at 05:39
  • @beroe I mention what I want to do. Your turn. – hamidfzm Nov 16 '13 at 07:30
  • Still not clear. You want there to be a single print statement capable of printing once then taking input(and store into variable) and then printing? Or do you want that functionality? If the functionality is required then explain what is required that cannot be achieved by 2 print statements and input in between? – Aseem Bansal Nov 16 '13 at 10:02
  • What is the problem you want to solve? This is a case of asking for help with a solution when the problem isn't clear. – Burhan Khalid Nov 16 '13 at 10:06
  • @Burhan Khalid Question edited. Is it clear enough to answer? – hamidfzm Nov 16 '13 at 10:41

4 Answers4

2

Not directly possible because assignment doesn't return anything in python but with a bit of hack if you really want.

def main():
    def f():
        nonlocal x
        x = input()
        return x
    x = ''
    print("Sin(" + f() + ")")

This example is an idea about how you can do it. Instead of nonlocal you can have other hacks like mutable objects passed directly to give you more control over where you want the value to be stored.

By mutable objects

As has been mentioned in comments it is not totally safe and you will need some changes before using this code. It is just meant as an example. Till it becomes clear what the OP actually wants I'll leave it as it is.

def main():
    def f(x):
        x[0] = input()
        return x[0]

    x = ['']
    print("Sin(" + f(x) + ")")

EDIT2 - Avoiding echo of newline like in case of input()

If you want to avoid newline to be echoed then you should see this. Then make a function to build the desired input into a nonlocal(or global) variable as per your requirements. Here you'll have to do some hackery to define another character at which the variable will be built completelty. Then using the function call between two print statements should give you the desired behavior. The variable returned by the function will have to be printed explicitly as nothing will be echoed.

Community
  • 1
  • 1
Aseem Bansal
  • 6,722
  • 13
  • 46
  • 84
  • +1. Nice alternative! – aIKid Nov 16 '13 at 06:05
  • @Aseem Bansal it's a tricky way but it seems not answering my question cause python first starts input command then print what is inputed! – hamidfzm Nov 16 '13 at 07:25
  • @HamidFzM So you want them to run in parallel? Or print, then input then print? If the latter then the only way I can think of is two print statements and input between these statements. – Aseem Bansal Nov 16 '13 at 07:48
  • @HamidFzM Can you give a practical reason where this could be useful or is it just a trick question? – Aseem Bansal Nov 16 '13 at 07:51
  • Quite ugly to simulate absence of `nonlocal` keyword with mutable method argument this way. Also a bit risky without checking if passed an empty list in `x`. – David Unric Nov 16 '13 at 08:43
  • @DavidUnric Correct. But as it is still not clear what the OP actually wants I'll leave it as is. – Aseem Bansal Nov 16 '13 at 09:25
  • @Aseem Bansal As you mentioned Could you say how to run in parallel? – hamidfzm Nov 16 '13 at 10:27
  • @HamidFzM Don't use things if you don't really need them. I was not able to understand the behavior you wanted and I had started blabbering. If I am correct you want to avoid the echoing of newline on the screen when something is inputted? If yes, it might be possible without external library. I'll take a look. – Aseem Bansal Nov 16 '13 at 17:30
0

No, you can't do that. Each statement has to be done manually:

x = float(input())
print("sin(%.2f)"%x)

Or if you want the sin result:

import math
x = math.sin(float(input("Enter X: ")))
print("The sin of X is: {:.2f}".format(x))

Edit: added what you want:

As i mentioned, you'll have to print each statement manually:

print("sin(", sep="")
#either this
x = float(input())
print(x)
#print(input()) #or just this
print(")")

Note the use of two different string formatting method, just to show it. Hope this helps!

aIKid
  • 26,968
  • 4
  • 39
  • 65
0

Here is some more concise way - oneliner:

print 'sin(%f)=%f' % (lambda x=input(): (x, sin(x)))()
# 1.57
# sin(1.570000)=1.000000
David Unric
  • 7,421
  • 1
  • 37
  • 65
  • I think he wants to store it in a variable also. – Aseem Bansal Nov 16 '13 at 06:06
  • @AseemBansal If that's what OP asked then this would require two phases - an assignment (it doesn not return a value) and string formatting expression. Not aware of built-in method which mimic such behaviour. – David Unric Nov 16 '13 at 08:48
0

You can't since you can not avoid newline character ath the end of input(). If you really need this, you should use a terminal control library like curses.

Info from here

Community
  • 1
  • 1
SzieberthAdam
  • 3,999
  • 2
  • 23
  • 31