0

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

Greg Peckory
  • 7,700
  • 21
  • 67
  • 114

7 Answers7

4

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.

Eric
  • 95,302
  • 53
  • 242
  • 374
2
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:")
Finglas
  • 15,518
  • 10
  • 56
  • 89
  • method no. 1 = ValueError: too many values to unpack (expected 5) method no. 2 does not seem to have any errors but python turtle window does not respond. The same thing seems to happen with all the other correct answers. Maybe it is not the script that is the problem. – Greg Peckory Jun 11 '12 at 18:53
2

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.

Finglas
  • 15,518
  • 10
  • 56
  • 89
Paul Whalen
  • 453
  • 6
  • 21
  • Note, the prints are not needed. Input can take a user prompt for the message. I updated your snippet to reflect this. – Finglas Jun 11 '12 at 18:39
  • Ha! Duh. Thanks, haven't done any user input with python in years. – Paul Whalen Jun 11 '12 at 18:53
  • this is very much how I would like to do it. Unfortunately I get an error here too. Maybe its a mistake somewhere else on my script. TypeError: Can't convert 'float' object to str implicitly – Greg Peckory Jun 11 '12 at 18:57
  • You need to convert the inputs, which are `str`ings into `float`s. So, add something like `x = float(x)` for each variable, following the `input()` calls. – Chinmay Kanchi Jun 11 '12 at 19:11
  • thanks for all the help and answers. In a way they were all right. I will be using Paul Whelans method of doing it. I had to make one small change to make it work right. What I did was: 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) Float also works instead of using int. So thank you in particular Paul Whelan for your answer :) – Greg Peckory Jun 11 '12 at 19:15
1

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.

Community
  • 1
  • 1
JustinDanielson
  • 3,155
  • 1
  • 19
  • 26
0
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.

JAB
  • 20,783
  • 6
  • 71
  • 80
0
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

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0
 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