Key takeaways
NOTE
You can test the functionality of the input() function in its full scope locally on your machine. For resource optimization reasons, we have limited the maximum program execution time in Edube to a few seconds. Go to the Sandbox, copy-paste the above snippet, run the program, and do nothing - just wait a few seconds to see what happens. Your program should be stopped automatically after a short moment. Now open IDLE, and run the same program there - can you see the difference?
Tip: the above-mentioned feature of the input() function can be used to prompt the user to end a program. Look at the code below:
name = input("Enter your name: ")
print("Hello, " + name + ". Nice to meet you!")
print("\nPress Enter to end the program.")
input()
print("THE END.")
4. The result of th einput() function is a string. You can add strings to each other using the concatenation (+) operator. Check out this code:
num_1 = input("Enter the first number: ") # Enter 12
num_2 = input("Enter the second number: ") # Enter 21
print(num_1 + num_2) # the program returns 1221
5. You can also multiply (* - replication) strings, e.g.:
my_input = input("Enter something: ") # Example input: hello
print(my_input * 3) # Expected output: hellohellohello
Exercise 1
What is the output of the following snippet?
x = int(input("Enter a number: ")) # The user enters 2
print(x * "5")
55
If the user enters 2 as the input, the output of the snippet will be the string "55". Here's why:
Exercise 2
What is the expected output of the following snippet?
x = input("Enter a number: ") # The user enters 2
print(type(x))
<class 'str'>
Here's why:
Congratulations! You have completed Module 2.
Well done! You've reached the end of Module 2 and completed a major milestone in your Python programming education.
Here's a short summary of the objectives you've covered and got familiar in Module 2:
You are now ready to take the module quiz and attempt the final challenge: Module 2 test, which will help gauge what you've learned so far.