简体   繁体   中英

How to use a function with an input?

Ok so I have a function I've made called DrawGraph. This one function is pretty much my whole program.

DrawGraph(x, y, xv, yv, s)

At the start of the program I would like to have to type in this functions. And input the specific numbers, x, y , xv, etc....

so I can enter different number every time I started the program.

What is the easiest way or even best way to do this? I cant expect it will be too hard.

What I've tried so far:

DrawGraph(x, y, xv, yv, s) = input()

DrawGraph(x, y, xv, yv, s) = input(x, y, xv, yv, s)

input(x, y, xv, yv, s)

......none of these worked.......

just want to throw in there. At the end of the program if I have no input but just

DrawGraph(5, 5, 10, 10, 60)

it works grand. When I start using an input it really gets messed up

You can just do:

input()

And type:

DrawGraph(0, 1, 2, 3, 4)

input takes any python expression and evaluates it. This can become a security hole.


A better approach would be to use raw_input() :

args = map(float, raw_input().split())
DrawGraph(*args)

That code takes the input, turns it into a list, and then turns each item into a float.

x, y, xv, yv, s = input()

The input would have to be in the form:

1,2,3,4,5

or

x = input()
y = input()
xv = input()
yv = input()
s = input()

The input for this would be in the form:

1
2
3
4
5

Either way, you would then need to pass the variables into your function.

Note that input can take a string parameter as the user prompt, for example:

x = input("Give me x:")

Unless you want to parse the values from one input, it would be easier to request each value and save it to a variable one at a time, and then calling your function.

x = input("Enter x:")
y = input("Enter y:")
xv = input("Enter xv:")
yv = input("Enter yv:")
s = input("Enter s:")

DrawGraph(x, y, xv, yv, s)

This will get the values from the user and then call the function with those values.

There is a good stack overflow question on user input in python here

You can accept the values as cmd line parameters, or use raw_input to obtain them.

args = [int(i) for i in raw_input().split()]

DrawGraph(*args)

For Python 3, you'd just replace raw_input() with input() . I'm sure there's a slightly easier way (aside from the potentially-dangerous usage of the old version of input() , as mentioned in Eric's answer), but I'm a little rushed at the moment and don't have time to think about it too much.

def do_draw_graph():
    x  = int(raw_input("Value for x? "))
    y  = int(raw_input("Value for y? "))
    xv = int(raw_input("Value for xv? "))
    yv = int(raw_input("Value for yv? "))
    s  = int(raw_input("Value for s? "))
    DrawGraph(x, y, xv, yv, s)

Works when using the turtle module in IDLE

 raw_input() is renamed to input() in Python3 

  x=int(input("enter x" )) 
  y=int(input("enter y" ))
  xv=int(input("enter xv" ))
  yv=int(input("enter yv" ))
  s=int(input("enter s" ))
  DrawGraph(x,y,xv,yv,s)

 it depends on what type of number are the inputs. if they'r decimals like 1.2 instead of int use float

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM