General

Tech

Anyone have Python experience?

I'm having trouble with my Python homework. Does anyone have any coding experience with python who can help me out?

Thanks,

Okay , so my homework assignment is to finish the coding to a number guessing game made by my instructor. I'm only going to paste the section I need help with. I took out the parenthesis/brackets in everything because it wouldn't let me post.

def SolicitInteger lobound=None, hibound=None, default_return=None :

"""Interactively asks the user to input an integer and returns that integer.
Arguments:
lobound -- an integer indicating the lowest acceptable user input. By default lbound is
assigned the value None, i.e., no lower bound on what's an acceptable integer.
hibound -- an integer indicating the highest acceptable user input. By default hibound is
assigned the value None, i.e., no upper bound on what's an acceptable integer.
default_return -- an integer indicating the value this function should return if the user
indicates s/he prefers the default by just pressing Enter.

Behavior:
* Print "invalid input" and return None if user input is not interpretable as an integer
or the user presses Enter and default_return is None.
* Print "too low" and return None if lbound is specified in the function call and
user input is less than lbound.
* Print "too high and return None if hibound is specified in the function call and
input is greater than hibound.
* Return default_return if it is specified in the function call and the user just
presses enter.
* Return the input converted to an integer if it passes all the criteria for valid input.

Tips:
* The integer 0 IS a legal value for a lower or upper bound.
* No loops in here. This function asks for input once and returns something based on that.
* I did it in 14 lines. Can you do it in less? :-)
"""

It is suppose to make this work:

def AskHowManyPlayers :
"""Prompts the user for an integer and checks for valid input."""

# Loop forever until the user enters an integer between 1 and 10, inclusive.
while True:
print "How many players? Enter a number between 1 and 10, or press enter for default 2:"
num_players = SolicitInteger lobound=1, hibound=10, default_return=2
if num_players != None:
print "Ok, players.".format num_players
return num_players

My answer was:
def SolicitInteger lobound=None, hibound=None, default_return=None :
value = raw_input
if value != None:
print "invalid input"
return None
if value < lobound:
print "too low"
return None
if value < lobound:
print "too high"
return None

But it doesn't work. The auto-grader my instructor coded to help us see if the game is working properly fails me on the "too low" and "too high" test, but it passes me on the "garbage input" test. Can anyone tell me what I'm doing wrong, and point me in the right direction?

March 5, 2014

10 Comments • Newest first

newrichboy

@Yumtoast: First time I've heard of pastebin lol I'll keep that in mind for next time. I figured out the homework though so don't worry about it Thanks anyways!

Reply March 7, 2014
Yumtoast

[quote=newrichboy]My answer was:
def SolicitInteger lobound=None, hibound=None, default_return=None :
value = raw_input
if value != None:
print "invalid input"
return None
[b]if value < lobound:[/b]
print "too low"
return None
[b]if value < lobound:[/b]
print "too high"
return None

But it doesn't work. The auto-grader my instructor coded to help us see if the game is working properly fails me on the "too low" and "too high" test, but it passes me on the "garbage input" test. Can anyone tell me what I'm doing wrong, and point me in the right direction?[/quote]
I didn't read the instructional pseudocode (you should have put your code on pastebin and linked it), but I have three questions:

Why is it an invalid input if value isn't equal to None? Is None supposed to be nothing (i.e. 0)? If it isn't, None is an incredibly confusing variable name.
Why are you checking if value is less than lobound multiple times?
Why are you returning None after each loop?

Reply March 7, 2014 - edited
newrichboy

@MarshMallows
@Suryoyo

Okay , so my homework assignment is to finish the coding to a number guessing game made by my instructor. I'm only going to paste the section I need help with. I took out the parenthesis/brackets in everything because it wouldn't let me post.

def SolicitInteger lobound=None, hibound=None, default_return=None :

"""Interactively asks the user to input an integer and returns that integer.
Arguments:
lobound -- an integer indicating the lowest acceptable user input. By default lbound is
assigned the value None, i.e., no lower bound on what's an acceptable integer.
hibound -- an integer indicating the highest acceptable user input. By default hibound is
assigned the value None, i.e., no upper bound on what's an acceptable integer.
default_return -- an integer indicating the value this function should return if the user
indicates s/he prefers the default by just pressing Enter.

Behavior:
* Print "invalid input" and return None if user input is not interpretable as an integer
or the user presses Enter and default_return is None.
* Print "too low" and return None if lbound is specified in the function call and
user input is less than lbound.
* Print "too high and return None if hibound is specified in the function call and
input is greater than hibound.
* Return default_return if it is specified in the function call and the user just
presses enter.
* Return the input converted to an integer if it passes all the criteria for valid input.

Tips:
* The integer 0 IS a legal value for a lower or upper bound.
* No loops in here. This function asks for input once and returns something based on that.
* I did it in 14 lines. Can you do it in less? :-)
"""

It is suppose to make this work:

def AskHowManyPlayers :
"""Prompts the user for an integer and checks for valid input."""

# Loop forever until the user enters an integer between 1 and 10, inclusive.
while True:
print "How many players? Enter a number between 1 and 10, or press enter for default 2:"
num_players = SolicitInteger lobound=1, hibound=10, default_return=2
if num_players != None:
print "Ok, players.".format num_players
return num_players

My answer was:
def SolicitInteger lobound=None, hibound=None, default_return=None :
value = raw_input
if value != None:
print "invalid input"
return None
if value < lobound:
print "too low"
return None
if value < lobound:
print "too high"
return None

But it doesn't work. The auto-grader my instructor coded to help us see if the game is working properly fails me on the "too low" and "too high" test, but it passes me on the "garbage input" test. Can anyone tell me what I'm doing wrong, and point me in the right direction?

Reply March 6, 2014 - edited
Yumtoast

[quote=MarshMallows]Python is literally one of the most efficient/easy to learn/easy to use programming languages EVER..[/quote]
Unfortunately, writing in a pseudocode language won't get you a job.

Reply March 6, 2014 - edited
iDrinkOJ

i only know beginner C, and some java but i forgot it. i did Python on codecademy.com but never completed it

Reply March 5, 2014 - edited
MarshMallows

[quote=Thiefy996]Python is such a pain in the ass.[/quote]

Python is literally one of the most efficient/easy to learn/easy to use programming languages EVER.
Like using a dictionary in Python is sooooooooo easy compared to Java.
And since you don't have to declare types in Python, it makes variables so much easier to use.
Unless you're one of those people who like to work in C or OCAML..then I applaud you my friend.

Reply March 5, 2014 - edited
CodeXLC

i have a pet python o.o

Reply March 5, 2014 - edited
Suryoyo

Well i finished my 2 semester long python introduction class and now learning how to program object oriented, so maybe i can help..

Reply March 5, 2014 - edited
SpiritBag

Well I spent like 12 hours yesterday doing a project and it was in python, so I have at least 12 hours of python experience.

Reply March 5, 2014 - edited
MarshMallows

There's actually an astounding amount of programmers on Basil - you could probably ask for functional programming/algorithms help, and people will still respond.
Anyways, what's the question/questions?

Reply March 5, 2014 - edited