User Input

E

Eternaltheft

Hi, I'm having trouble oh how prompt the user to enter a file name and how to set up conditions. For example, if there's no file name input by the user, a default is returned
 
F

Fábio Santos

Hi, I'm having trouble oh how prompt the user to enter a file name and
how to set up conditions. For example, if there's no file name input by the
user, a default is returned

Are you using raw_input? It returns an empty string if the user enters
nothing, so you can just use an if.

filename = raw_input('file name: ')
if not filename:
filename = 'your default'
 
E

Eternaltheft

Hi, I'm having trouble oh how prompt the user to enter a file name and how to set up conditions. For example, if there's no file name input by the user, a default is returned

Thanks for such a fast reply! and no im not using raw input, im just using input. does raw_input work on python 3?
 
M

MRAB

Thanks for such a fast reply! and no im not using raw input, im just
using input. does raw_input work on python 3?
In Python 2 it's called "raw_input" and in Python 3 it's called "input".

Python 2 does have a function called "input", but it's not recommended
(it's dangerous because it's equivalent to "eval(raw_input())", which
will evaluate _whatever_ is entered).
 
F

Fábio Santos

how to set up conditions. For example, if there's no file name input by the
user, a default is returned
Thanks for such a fast reply! and no im not using raw input, im just
using input. does raw_input work on python 3?

On python 2, the function to prompt the user for input and return a string
is raw_input.

On python 3 that function has been renamed to input.

However on python 2 input is something else. It also evaluates the input as
a python expression. That makes it unsafe to use in most circumstances.
 
E

Eternaltheft

Ok thanks guys. but when i use

filename = input('file name: ')
if not filename: #i get filename is not defined
return(drawBoard) #possible to return function when no file input from user?
 
F

Fábio Santos

Ok thanks guys. but when i use

filename = input('file name: ')
if not filename: #i get filename is not defined
return(drawBoard) #possible to return function when no file input
from user?

I don't really understand what you mean. Do you mean that you're getting a
stack trace?
 
C

Chris Angelico

Ok thanks guys. but when i use

filename = input('file name: ')
if not filename: #i get filename is not defined
return(drawBoard) #possible to return function when no file input from user?

Do you really want to return there? What function is this defined in?

I think you probably want what Fabio originally said: that "if not
filename" (which, in this context, means "if the user hit Enter
without typing anything"), assign something to filename.

ChrisA
 
E

Eternaltheft

sorry about that, i got confused xD. yeah it works good now.
what i meant to say was can i return a function that i made, if the user inputs nothing?
 
D

Dave Angel

sorry about that, i got confused xD. yeah it works good now.
what i meant to say was can i return a function that i made, if the user inputs nothing?

There wouldn't be anything to stop you. However, if you have multiple
returns from the same function, it's usually wise to return the same
type of information from each of them. That's why Chris suggested
simply assigning to filename in the if clause.

But without the whole function, and maybe even a description of what the
function is expected to do, we can only guess.

Your comments still make no sense to me,
filename = input('file name: ')
if not filename: #i get filename is not defined

But filename IS defined, immediately above. If it were undefined, you'd
not be able to test it here. Big difference between "not defined" and
"is empty string".
return(drawBoard) #possible to return function when no file input
from user?

If drawBoard is a function object, it's certainly possible to return it.
But again, without seeing the rest of the function, and maybe how it's
intended to be used, I can't confirm whether it makes sense.
 
C

Chris Angelico

sorry about that, i got confused xD. yeah it works good now.
what i meant to say was can i return a function that i made, if the user inputs nothing?

Sure! Anything you want to do, you can do :)

ChrisA
 
E

Eternaltheft

yeah i found out why it wasn't defined before because i tried to put it into a function.

this is my drawBoard function:

import turtle as Turtle
Turtle.title("Checkers")
b = 75

def drawBoard(b):

Turtle.speed(0)

Turtle.up()

Turtle.goto(-4 * b, 4 * b)
Turtle.down()

for i in range (8):
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)

for i in range (1):
Turtle.right(90)
Turtle.forward(b*2)
Turtle.right(90)

for i in range(8):
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)

for i in range(1):
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)

for i in range (8):
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)

for i in range (1):
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)

for i in range(8):
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)

for i in range(1):
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)

for i in range (8):
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)

for i in range (1):
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)

for i in range(8):
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)

for i in range(1):
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)

for i in range (8):
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)

for i in range (1):
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)

for i in range(8):
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)


drawBoard(b)

Turtle.done()

it draws an 8x8 board table.

what i initially wanted to do was to return this function if nothing was inputted from the user. i hope that makes more sense :S
 
D

Dave Angel

yeah i found out why it wasn't defined before because i tried to put it into a function.

That's not a sentence, and it doesn't make sense in any permutation I
can do on it.
this is my drawBoard function:

import turtle as Turtle
Turtle.title("Checkers")
b = 75

def drawBoard(b):

Turtle.speed(0)

Turtle.up()

Turtle.goto(-4 * b, 4 * b)
Turtle.down()

for i in range (8):
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)

for i in range (1):
Turtle.right(90)
Turtle.forward(b*2)
Turtle.right(90)

for i in range(8):
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)

for i in range(1):
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)

for i in range (8):
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)

for i in range (1):
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)

for i in range(8):
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)

for i in range(1):
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)

for i in range (8):
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)

for i in range (1):
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)

for i in range(8):
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)

for i in range(1):
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)

for i in range (8):
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)
Turtle.left(90)
Turtle.forward(b)

for i in range (1):
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)

for i in range(8):
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)
Turtle.right(90)
Turtle.forward(b)


drawBoard(b)

Turtle.done()

it draws an 8x8 board table.

what i initially wanted to do was to return this function if nothing was inputted from the user. i hope that makes more sense :S

It makes sense if you're also returning a function object when the user
does have something to say. But I can't see how you might be doing
that, unless you're using a lambda to make up a custom function object.

And you still don't show us the function that contains this input
statement. Nor how it gets used. Is the user supposed to supply a
value for b, and you put it in a variable called filename ?

And perhaps you meant for your function to CALL drawBoard(), rather than
returning the function object drawBoard.
 
E

Eternaltheft

And perhaps you meant for your function to CALL drawBoard(), rather than

returning the function object drawBoard.

DaveA


do you think it would be better if i call drawBoard?
 
E

Eternaltheft

And perhaps you meant for your function to CALL drawBoard(), rather than
returning the function object drawBoard.
DaveA

do you think it would be better if i call drawBoard?
 
J

Joshua Landau

do you think it would be better if i call drawBoard?

Please read http://www.catb.org/esr/faqs/smart-questions.html, or
anything similar you can find.

Start from the beginning.

1) What are you doing? Not "what are you doing now" but, from the top,
what is the goal you are trying to achieve?

2) How have you tried to do it? Code would be nice here too, but
don't just send really large blocks of irrelevant code. For example, your
"drawBoard" function would be better surmised as:

def drawBoard(b):
Turtle.speed(0)
Turtle.up()
Turtle.goto(-4 * b, 4 * b)
Turtle.down()

for i in range (8):
Turtle.forward(b)
Turtle.right(90)
... # etc, drawing a board

3) What are you stuck on? In this case, you are stuck on what to do
after you call input("stuff"), if I understand. What do you want to do
- not "how do you want to do it" but what is it that you are doing?

4) Finally, we should understand what calling drawBoard is for. Ask us
again and we'll be much more likely to give good answers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top