0

I'm trying to make a factorial calculator.

Input and Expected output:: If the user input is a positive integer, I want the program to give the outcome. And if the user input is not a positive integer(negative integer, float, or string), I want the program to ask for the input again. Also, I want the program to end when the user input is 0.

Problem: Since inputs are always perceived as string data, I am stuck with coding according to the type of input.

It would be of great help if someone would give answers to this problem, as I am studying by myself.

martineau
  • 119,623
  • 25
  • 170
  • 301
Gina
  • 31
  • 2
  • This is already answered elsewhere https://stackoverflow.com/a/43564344/2308683.. https://stackoverflow.com/a/34797270/2308683 – OneCricketeer Aug 26 '17 at 14:56

3 Answers3

1

If you want to ensure it's a positive integer, and if you want to keep asking for input as specified in your question, you'll need a few more control structures:

from math import factorial

def get_input():
    while True:
        try:
            inp = int(input("Enter a positive integer> "))
            if inp < 0:
                raise ValueError("not a positive integer")
        except ValueError as ve:
            print(ve)
            continue
        break

    return inp

print(factorial(get_input()))

This works by just trying to convert the input to an integer, and retrying if this fails. The continue statement is used to skip past the break. The try/except structure catches the error if it's not an integer, or the error explicitly raised if it's less than 0. It also uses the as feature of except to print a better indication of the error. It's encapsulated in a function to make things easier - I find factorial(get_input()) to be pretty expressive, with the added bonus of being more reusable.

This currently doesn't end when the user input is 0, as 0 is a perfectly valid input for factorial, although it should be easy enough to adapt to this with an if statement.

This program might be used like this:

Enter a positive integer> abc
invalid literal for int() with base 10: 'abc'
Enter a positive integer> 0.2
invalid literal for int() with base 10: '0.2'
Enter a positive integer> -5
not a positive integer
Enter a positive integer> 6
720

By the way, this code works according to EAFP - it just tries to convert to an integer, and handles failure. This is more idiomatic Python than first trying to determine if it could be an integer (LBYL).

If you're using Python 2, you'll need to change input to raw_input.

Izaak van Dongen
  • 2,450
  • 13
  • 23
0

Try to think of it as an algorithm problem not as a Python problem, you may find a solution because in general every language has its functions and they all almost the same only with different name. In python they are a function call isnumeric() and it returns true if every single character is a number or false if it's not.

str.isnumeric()

us it with a statement condition like if

if str.isnumeric()==true // so it's numeric
  // what to do!!!!!

the best choice it to use while and if

while true
   //read data as string
   if str.isnumeric()==true
      break; // this to break the while loop

hope this can help you

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
shadow
  • 767
  • 3
  • 8
  • 20
  • Your code isn't really Python.. Python comments use `#`, the Python literal for the 'true' Boolean is `True`, you need `:` before your indents, and rather than checking `if condition==true`, you can use `if condition`. – Izaak van Dongen Aug 26 '17 at 15:25
  • yeah i know, i'm not a python programmer, i said it's like an algorithm problem and every language has its own function for most of this kind of problem, isnumeric() it's a python function. he can use it with while if. python doesn't have do..while condition so he need to use it like this while ... if (cindition){ break:} – shadow Aug 26 '17 at 15:26
  • In that case, as an algorithm this falls under LBYL. In Python, AFAIK, it's generally considered better to use an [EAFP](https://docs.python.org/2/glossary.html#term-eafp) approach for this kind of question, so in a sense it is 'a Python problem' by the very nature of its algorithm, in addition to needing Python syntax. – Izaak van Dongen Aug 26 '17 at 15:32
  • 1
    well i can't argue too much with you, you are much better then me in python, and as i can see, you are right about using EAFP approach, but this is don't mean that the other way will not work (LBYL), it's like 2 ways for the same solution. the best it's yours , thank you for the information by the way. what i tried to say, he can use isnumeric() as a solution, this will not give him information about the input if it's float negative or string, it works only if it's a positive integer. – shadow Aug 26 '17 at 15:45
0

Check if input is positive integer check this link out along with the link provided by user @cricket_007 above. Combining those two information should get you in the right direction.

Joseph K.
  • 1,055
  • 3
  • 23
  • 46