How to make simple user input in python?

I just play with input elements and variables. I am trying to run a simple function:

slope = (y2-y1)/(x2-x1)

I would suggest the user to input y2, y1, x2and x1. What is the easiest, cleanest way to do this?

+3
source share
4 answers

Use raw_input()for Python 2 series:

x1 = float(raw_input("x1: "))
y1 = float(raw_input("y1: "))
x2 = float(raw_input("x2: "))
y2 = float(raw_input("y2: "))

In Python 3 series use input().

+15
source

This is the easiest way:

 x1 = float(raw_input("Enter x1: "))

Note that the function raw_input()returns a string that is converted to a floating point number with float(). If you type something other than a number, you will get an exception:

>>> float(raw_input())
a
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for float(): a

Python 3 (, ), input raw_input.

+3

:

foo=input('Please enter a value:')

", :" , foo .

0

:

:

, :

n, m = raw_input(). split ('')

val1, val2, val3 = raw_input(). split ('')

:

val = float (val1) ...

The trick is that in this way you do not waste your space on creating a new list and save your values ​​in it, but do not get it.

0
source

Source: https://habr.com/ru/post/1786279/


All Articles