-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_guid.py
More file actions
50 lines (37 loc) · 1.47 KB
/
input_guid.py
File metadata and controls
50 lines (37 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# INPUT FUNCTION – FULL USAGE GUIDE
# Basic Syntax:
# input(prompt='')
# Parameters:
# prompt → A string, written to standard output without a trailing newline,
# to ask the user for input. Default is an empty string ''.
# Returns → A string entered by the user (always str type).
# Notes → Always returns a string. You need to convert it using int(), float(), etc. if needed.
# --------------------------------------------
# 1. Basic usage with no prompt
user_input = input()
print("You entered:", user_input)
# 2. Input with a prompt
name = input("Enter your name: ")
print("Hello,", name)
# 3. Converting input to integer
age = int(input("Enter your age: "))
print("You will be", age + 1, "next year.")
# 4. Converting input to float
height = float(input("Enter your height in meters: "))
print("Your height in cm is", height * 100)
# 5. Reading multiple values (as strings)
x, y = input("Enter two words separated by space: ").split()
print("Word 1:", x)
print("Word 2:", y)
# 6. Reading and converting multiple values to int
a, b = map(int, input("Enter two integers: ").split())
print("Sum =", a + b)
# 7. Reading many values into a list of ints
numbers = list(map(int, input("Enter multiple numbers: ").split()))
print("You entered:", numbers)
# 8. Handling invalid input using try/except
try:
salary = float(input("Enter your monthly salary: "))
print("Yearly salary:", salary * 12)
except ValueError:
print("Invalid input! Please enter a number.")