Class 2 Lab 1 ‐ Working with User Input - Justin-Boyd/Python-Class GitHub Wiki
Step 1: Create two variables that accept input from the user.
x_string = input(“Enter 1st number:”)
y_string = input(“Enter 2nd number:”)
Step 2: Add the two variables, and print the results.
- Why does the console print a combined result of the inputs instead of the mathematical operation?
print(“Sum: “, x_string + y_string)
Step 3: Use the int() function to cast your variables from the String data type to the Integer data type.
x = int(x_string)
y = int(y_string)
Step 4: Add the two variables (x and y) and print the results to the console.
- (Note that the two variables are integers and are mathematically added.)
print(“Sum: “, x+y)
Step 5: Subtract the two variables and print the results to the console
print(“Difference: “, x-y)
Step 6: Multiply the two variables and print the results to the console.
print(“Multiplication: “, x*y)
Step 7: Divide the two variables and print the results to the console.
print(“Division: “, x/y)
Step 8: Perform a modulo operation on the variables and print the results to the console.
print(“Remainder: “, x%y)
Full Code
x_string = input("Enter 1st number:")
y_string = input("Enter 2nd number:")
print("Sum: ", x_string + y_string)
x = float(x_string)
y = float(y_string)
print("Sum: ", x+y)
print("Difference: ", x-y)
print("Multiplication: ", x*y)
print("Division: ", x/y)
print("Division: ", x/y)