I write a simple factorial function in python to know the factorial of n . As like we given n and it shows the factorial of n . But the problem is , i use raw_input function to get the value of n. But can't work with that . What can i do to do with raw_input ?
            Asked
            
        
        
            Active
            
        
            Viewed 201 times
        
    0
            
            
        - 
                    1Paste your code, so that people can understand your issue clearly.. – Ravi Kumar Mar 26 '16 at 05:58
- 
                    Can you accept my answer if you find it correct?! I also suggested the same what @XYZ Programmer suggested, and I answered before him. – Ayush Mishra Mar 28 '16 at 02:26
2 Answers
1
            
            
        Your factorial function probably takes an integer as input.
However, raw_input returns a string.
So you either need to convert the returned string to an integer. By using int().
Or you can directly use input() which returns an integer in Python2.
 
    
    
        Ayush Mishra
        
- 506
- 1
- 4
- 15
- 
                    `input()` returns a string in python 3 so your last line makes no sense (plus using`input()` in python 2 is a really bad idea) – Tadhg McDonald-Jensen Mar 26 '16 at 06:07
- 
                    My bad, I will edit it. But why's using input() in python2 a really bad idea? – Ayush Mishra Mar 26 '16 at 06:10
- 
                    1because input wil evaluate whatever the user inputs as a python line. So if they do exit(), for example, the program closes, just like that. – Dleep Mar 26 '16 at 06:19
- 
                    @AyushMishra if you type in `with open(__file__,"w") as f:f.write("all gone")` from a python2 `input()` it deletes your program file – Tadhg McDonald-Jensen Mar 27 '16 at 17:39
- 
                    sorry that would raise a SyntaxError, if you wrote `open(__file__,"w").close()` it would erase the file. – Tadhg McDonald-Jensen Mar 27 '16 at 17:50
- 
                    
0
            You need to cast the default string type to int
val = raw_input()    #val accepts a string 
val = int(raw_input())   #val takes integer input
 
    
    
        XZ6H
        
- 1,779
- 19
- 25
- 
                    
- 
                    You could have at least provided an example supporting your answer instead of down voting. – XZ6H Mar 26 '16 at 06:34
 
    