Programs that don't calculate at least something are quite rare. Thus, learning to…
- User
Programs that don't calculate at least something are quite rare. Thus, learning to program with numbers is always a good idea. переведи
- Assistant
Программы, которые не выполняют хотя бы какие-то вычисления, довольно редки. Поэтому изучение программирования с числами всегда является хорошей идеей.
- User
Since you have become familiar with the input() function in Python, it's hardly new to you that any data passed to this function is treated as a string.
But how should we deal with numerical values? As a general rule, they are explicitly converted to corresponding numerical types. To do it, we add data type before input:
- Assistant
Since you have become familiar with the input() function in Python, it's hardly new to you that any data passed to this function is treated as a string.
But how should we deal with numerical values? As a general rule, they are explicitly converted to the corresponding numerical types. To do this, we can add a data type conversion function before the input. For example, to convert input to an integer or a float, you would do the following:
# To get an integer input: number = int(input("Enter an integer: ")) # To get a float input: decimal_number = float(input("Enter a decimal number: "))This way, we ensure that the data we work with is in the correct numerical format.
- User
переведи на русский Since you have become familiar with the input() function in Python, it's hardly new to you that any data passed to this function is treated as a string.
But how should we deal with numerical values? As a general rule, they are explicitly converted to corresponding numerical types. To do it, we add data type before input:
- Assistant
Поскольку вы уже знакомы с функцией input() в Python, вам едва ли ново, что любые данные, переданные в эту функцию, воспринимаются как строка.
Но как нам следует иметь дело с числовыми значениями? Как правило, они явно преобразуются в соответствующие числовые типы. Для этого мы добавляем преобразование типа данных перед input:
- User
We should consider user mistakes: if a user types an incorrect input, like the string 'two' instead of the number 2, an error (ValueError, to be precise) will occur. We'll learn how to handle such scenarios in the future.
For now, let's focus on what we can control. So, let's correct the code below so that it outputs the sum of the two integers the user inputs. As mentioned, we will assume the user correctly enters integers in the input field.
💡Hint: Correctly convert the type of the input data.
перевод
- Assistant
Мы должны учитывать ошибки пользователя: если пользователь введет неправильные данные, например, строку 'two' вместо числа 2, произойдет ошибка (конкретно, ValueError). Мы узнаем, как обрабатывать такие сценарии в будущем.
Пока давайте сосредоточимся на том, что мы можем контролировать. Итак, давайте исправим код ниже, чтобы он выводил сумму двух целых чисел, которые вводит пользователь. Как упоминалось, мы будем предполагать, что пользователь корректно вводит целые числа в поле ввода.
💡Совет: Правильно преобразуйте тип вводимых данных.