# Ask the user for input
user_input = input("Please enter your name: ")
# Display the user's input
print(f"Hello, {user_input}!")
The input() function prompts the user to enter something and returns the entered value as a string.
The print() function displays the value of the user_input variable to the console.
When you run this program, it will ask the user to enter their name and then display the user's name. For example, if the user enters "John Doe", the output will be:
Please enter your name: John Doe
Hello, John Doe!
# Ask the user for their username and password
username = input("Please enter your username: ")
password = input("Please enter your password: ")
# Check if the username and password are correct
if username == "User" and password == "Pass":
print("Welcome")
else:
print("No access")
The input() function prompts the user to enter their username and password, respectively.
The if statement checks if the entered username (username) and password (password) are "User" and "Pass", respectively. If they match, the program prints "Welcome".
If the username and password do not match, the program prints "No access".
When you run this program, it will ask the user to enter their username and password, and then display whether they were able to log in. If the username or password is incorrect, it will print "No access". If they are correct, it will print "Welcome".
2. Opening HTML page with python.
To open an HTML page with a Python program, you can use the requests library to send an HTTP request to the specified URL. Below is a simple example of how you can achieve this:
Install the requests library if you haven't already:
pip install requests
Create a Python program that prompts the user for an URL and opens the HTML page.
Here's a basic example:
Python
import requests
def open_html_page(url):
try:
response = requests.get(url)
if response.status_code == 200:
print("HTML page opened successfully.")
with open('html_page.html', 'w', encoding='utf-8') as file:
file.write(response.text)
else:
print("Failed to open the page. Status code:", response.status_code)
except Exception as e:
print("An error occurred:", e)
def main():
url = input("Enter the URL of the HTML page: ")
open_html_page(url)
if __name__ == "__main__":
main()
The requests.get(url) function sends an HTTP GET request to the specified URL.
If the status code of the response is 200, it indicates that the request was successful, and the HTML page is open.
If the status code is not 200, it indicates an error occurred, and a Exception is raised.
The HTML page is saved to a file named html_page.html using the with statement.
Save the code in a Python file, for example, open_html_page.py.
Run the script using Python:
python open_html_page.py
Enter the URL of the HTML page when prompted.
The requests.get(url) function sends a GET request to the provided URL.
If the response status is 200, it means the request was successful, and the HTML page is open.
If the status code is not 200, it means the request failed, and an error occurs.
This is a basic example and can be expanded with more complex HTML handling, error handling, and more features.