Using_Python_to_Interact_wi.../module4.ipynb
2025-01-24 23:24:46 +03:00

71 lines
2.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Reading data interactively"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cat hello.py\n",
"#!/usr/bin/env python3\n",
"\n",
"name = input(\"Please enter your name: \")\n",
"print(\"Hello, \" + name)\n",
"\n",
"# ./hello.py \n",
"# Please enter your name: Roger\n",
"# #Output will be Hello, Roger"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# defining a function called 'to_seconds' which takes hours, minutes and seconds as\n",
"# inputs and returns their total equivalent in seconds by multiplying each with its\n",
"# corresponding factor (3600 for hours, 60 for minutes) and then summing them up.\n",
"def to_seconds(hours, minutes, seconds):\n",
" return hours*3600+minutes*60+seconds\n",
"\n",
"# Print a welcome message to the user\n",
"print(\"Welcome to this time converter\")\n",
"\n",
"# set 'cont' variable to \"y\" initially for entering into while loop.\n",
"cont = \"y\"\n",
"\n",
"# while condition is true (user wants to continue), keep asking the\n",
"# number of hours, minutes and seconds from the user and print their total equivalent\n",
"# in seconds by calling 'to_seconds' function. Ask if they want to do another conversion.\n",
"# If not, break the loop.\n",
"while(cont.lower() == \"y\"):\n",
" hours = int(input(\"Enter the number of hours: \"))\n",
" minutes = int(input(\"Enter the number of minutes: \"))\n",
" seconds = int(input(\"Enter the number of seconds: \"))\n",
"\n",
" print(\"That's {} seconds\".format(to_seconds(hours, minutes, seconds)))\n",
" print()\n",
" cont = input(\"Do you want to do another conversion? [y to continue] \")\n",
" \n",
"# print a goodbye message when user decides to stop the program.\n",
"print(\"Goodbye!\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}