[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

I'm trying to do this code, can somebody help me

On Sat, Feb 15, 2014 at 3:27 PM,  <pabloeruggeri at gmail.com> wrote:
> 1)Given the variables  x , y , and z , each associated with an int , write a fragment of code that assigns the smallest of these to min

This is homework. Please be honest about it; we will help you to
learn, but we won't write your code for you.

> this is what I have
> def main():
>  x = eval ( input ("dame el numero X"))
>  y = eval ( input ("dame el numero Y"))
>  z = eval ( input ("dame el numero Z"))

Using eval is slow and dangerous; what you probably want here is simply int().

>  if(x>y and z>y):
>   min=y
>   print(min)

Oh, you're so close here :) The name "min" should be a clue. If you
type this into a Python-sensitive editor like IDLE, it might even be
highlighted in a different color.

>  else:
>   if(y>x and z>x):
>     min=x
>     print(min)
>   else:
>    min=z
>    print(min)
> main()

Okay. So why are you posting your code? Is it doing the wrong thing?
Is it throwing an exception? Is it sneaking out and kissing your
girlfriend? (I will never tolerate a program doing that, especially as
it would imply that I have a girlfriend.) Is it working perfectly but
you'd like to see it shorter? Are you hoping for a general code
review? Ask a question regarding the code.

Since you haven't asked anything else, I'll make a few stylistic
recommendations. Firstly, indenting by a single space is a bit too
compact; it's hard to see exactly how far in you are. I like to use
one tab for each indentation level; some prefer to use four spaces, or
eight spaces. Two spaces is usually considered too tight; more than
eight (or more than one tab) is extremely sparse; three, five, six,
and seven are just weird. Four or eight spaces, or exactly one tab,
would be the most common.

Also, putting your code inside main() and then immediately calling
main() doesn't help you much. If you want your module to be importable
from elsewhere, you'll need to check __name__, but I suspect you
haven't learned that yet. Otherwise, just put all your code at top
level and avoid the boiler-plate.

Instead of nesting 'if' inside 'else', you can merge the two into a
single 'elif', which makes good sense here.

What happens if two of your inputted numbers are equal?