input from user

前端 未结 4 1013
耶瑟儿~
耶瑟儿~ 2021-01-23 04:45

It\'s a very basic doubt in Python in getting user input, does Python takes any input as string and to use it for calculation we have to change it to integer or what? In the fol

相关标签:
4条回答
  • 2021-01-23 05:15

    raw_input() stores the inputted string from user as a "string format" after stripping the trailing new line character (when you hit enter). You are using mathematical operations on string format that's why getting these errors, first cast your input string into some int variable by using a = int(a) and b = int(b) then apply these operations.

    0 讨论(0)
  • 2021-01-23 05:25
    a = input("Enter integer 1: ")
    b = input("Enter integer 2: ")
    
    c=a+b
    d=a-b
    p=a*b
    print "sum =", c
    print "difference = ", d
    print "product = ", p  
    

    Just use input() and you will get the right results. raw_input take input as String.

    And one more i Would Like to Add.. why to use 3 extra variables ?

    Just try:

    print "Sum =", a + b
    print "Difference = ", a - b
    print "Product = ", a * b 
    

    Don't make the Code complex.

    0 讨论(0)
  • 2021-01-23 05:36

    Yes, you are correct thinking you need to change the input from string to integer.

    Replace a = raw_input("Enter the first no: ") with a = int(raw_input("Enter the first no: ")).

    Note that this will raise a ValueError if the input given is not an integer. See this for how to handle exceptions like this (or use isnumeric() for checking if a string is a number).

    Also, beware that you although you might find that replacing raw_input with input might work, it is a bad and unsafe method because in Python 2.x it evaluates the input (although in Python 3.x raw_input is replaced with input).

    Example code could therefore be:

    try:
        a = int(raw_input("Enter the first no: "))
        b = int(raw_input("Enter the second no: "))
    except ValueError:
        a = default_value1
        b = default_value2
        print "Invalid input"
    
    c = a+b
    d = a-b
    p = a*b
    print "sum = ", c
    print "difference = ", d
    print "product = ", p  
    
    0 讨论(0)
  • 2021-01-23 05:37

    Yes, every input is string. But just try:

    a = int(a)
    b = int(b)
    

    before your code.

    But be aware of the fact, that user can pass any string he likes with raw_input. The safe method is try/except block.

    try:
        a = int(a)
        b = int(b)
    except ValueError:
        raise Exception("Please, insert a number") #or any other handling
    

    So it could be like:

    try:
        a = int(a)
        b = int(b)
    except ValueError:
        raise Exception("Please, insert a number") #or any other handling
    c=a+b
    d=a-b
    p=a*b
    print "sum =", c
    print "difference = ", d
    print "product = ", p  
    

    From the documentaion:

    The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

    0 讨论(0)
提交回复
热议问题