1 Replies - 883 Views - Last Post: 30 October 2012 - 09:36 AM Rate Topic: -----

#1 chan 06   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 07-October 12

[Help] How to add arguement

Posted 30 October 2012 - 07:15 AM

x, y, j, a, = 0,0,0,0

x=int(input("Enter a positive integer value:"))

if (x==0):
    print("Please input an integer.")
    a = x
    b = x

elif (str(x)):
    print("Please input an integer.")
    a = x
    b = x

elif (float(x)):
    print("Please input an integer.")
    a = x
    b = x

elif (x<0):
    print("Wrong value entered.")

    print("Additive combination(s) of your number, " + str(x) + " is/are: ")

#Create a For Loop for a range between 0 to x
for i in range (0, x+1):
    print("(" + str(i) + "," + str(x) + ")",end="",)
    x = x-1
        print(", ",end="",)

x = a
y = a

print("\n\nMultiple combination(s) of your number", str(y) + " is/are: ")
for j in range (1,y+1):
    if (y%j == 0):
        a = int(y/j)
        print("(" + str(j) +"," + str(a) + ")",end="")
            print(", ",end="")

Well to start off, I would like to be saying thanks for people who have been helping me in program. I did learn some from my mistakes and hope not to repeat them, so once again thanks for helping since you guys are taking your time to help others out like me.

So to start with, I am making a program that create arguments that disallow user entering variables such as:
- Negative values
- Zero
- Float
- String
- Combinations float/string (1.7a) or negative value/string (-281.018BaU), etc...

Also my output statement is:

Please input an integer
For i loop (x,y)

Multiple Combination(s) of your number (integer) is/are:
For j loop (x,y)

I am not sure what is wrong with my argument, why is not working, and my program breaks when I try to enter string & float, even though I was asked to make the user to re-enter the value again.

Is This A Good Question/Topic? 0
  • +

Replies To: [Help] How to add arguement

#2 Tayacan   User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: [Help] How to add arguement

Posted 30 October 2012 - 09:36 AM

This line:
x=int(input("Enter a positive integer value:"))

Takes some input from the user, and immediately converts it to an integer. If the input is something that can't be converted to an int, an exception is thrown.

This line:
elif (str(x)):

First tries to convert x to a string, then asks if that string evaluates to True. In Python, a string is True is it's not empty, and False if it is. So "" is False, while any other string is True.

The same thing goes for
elif (float(x)):

Except that for floats, 0.0 is False, and anything else is True.

Now we've fixed the obvious errors, so on to how to solve the problem. First of all, you'll want to just take your input and save it as a string, then start figuring out what's in it. If you're using Python 3.x, just use the input() function.
s = input("Enter a positive integer value: ")

If you're using Python 2.x, use raw_input() instead:
s = raw_input("Enter a positive integer value: ")

Okay, so now we just need to figure out if the string contains a positive integer. There are several ways to do this, some which are complicated but flexible, others that are simple but pretty useless for anything more complex. I'll explain two of the simple ones here.

First, you can use a try-except. It looks like this:
    x = int(s)
except ValueError:
    # whatever should happen when it fails

So what does it do? Well, first, it tries to convert s to an int, and save it in the variable x. If that succeeds, it happily continues with whatever code is after the except block. However, if it fails - as it will if you try something like int("5.2"), it goes into the except block and runs whatever code is there.

Notice that the above code only handles ValueErrors. That's the kind of error you get from calling int() on something that can't be an int, like "5.2". If it throws any other kind of error, the program will still crash.

Now, the other (simple) way to do this, without the messy try-except block, is to simply check if all the characters in the strings are digits. That's more or less the definition of the string representation of an integer, right? So how to do this?

Well, how about we start in the official python documentation? If you click that link, find the search bar, and search for "isdigit", then you should find something useful.

Good luck, and do ask if you need more help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1