How To Create A Program In Python
Download Article
Download Article
Python is a simple yet powerful programming language to learn. It accommodates all levels of programmers from beginners to advanced programmers. Python is flexible and can work on available operating systems e.g., Mac, Windows, and Linux. Have you learned about the bare basics of python but are confused on how to apply them? Well, this article shows you how to make a program that calculates your total days, minutes, and seconds you have been alive! It is a very simple program that demonstrates how some things work in this programming language. Note that this is for users who have a basic understanding of python.
Steps
-
1
Open up a new window in python shell by pressing ctrl-N or going to 'File' and 'New window'.
-
2
Start with an introductory sentence. So you have to use the print function. Type below codes:
print ( "Let's see how long you have lived in days, minutes and seconds." )
-
3
Ask the user's name. It's nice to know what the user's name is, so type this in line 2:
- The variable "name" has now been replaced by the user's input.
-
4
Ask their age. You need to know the age, now you do the same thing as above except you have to use the "int" function, because the user will enter a number, like this:
- The variable "age" has now been replaced by the user's input.
print ( "now enter your age" ) age = int ( input ( "age: " ))
-
5
Do the conversions using the user's given age.
- Once you have written this, Python automatically changes the values for days, minutes, and seconds, based on the user's input of age.
days = age * 365 minutes = age * 525948 seconds = age * 31556926
-
6
Display to the user his/her information.
print ( name , "has been alive for" , days , "days" , minutes , "minutes and" , seconds , "seconds! Wow!" )
-
7
Congrats! you made a real program that serves a purpose! Save it and run it by going to 'run' and 'run module'. Try it out for yourself!
Add New Question
-
Question
What do I do if I get errors while trying to create a simple program?
Try to check and read over your code. Are your variable names correct? Are there any misused brackets or symbols? Usually it will be highlighted if this is the case. If it is, try to figure out how to fix it the best you can.
-
Question
The program closes after I type my name and never shows me how many seconds I lived. I copied and pasted everything here. I am using Python 2.7 and can't get a newer version as I use Win XP. Do you have any suggestions for me?
Under python 2.7, 'input()' asks for an input to execute (a python statement). The exemple above is for python 3 which disable the direct execution of code via 'input()'. So, if you need the user to give you an input, use 'raw_input()' instead of 'input()'
-
Question
How do I write a comment?
Cluster Duck
Community Answer
There are two ways to write a comment in python. A # is used for a one line comment. A """ is used for a multi line comment.
-
Question
How can I export my Python programs as a shareable file?
Copy them into a notepad file and send that, then let your friend or whoever copy/paste it into his/her Python program.
-
Question
How can I write a Python program that outputs all prime numbers?
Prime.py print(1) for i in range (2, 101): x = 1 for j in range (2, i): n = i % j if n == 0: x = 0 break if x ==1: print(i) This should give you an output in prime numbers for prime numbers between 1 and 100. You can expand your results by simply increasing the value in your range.
-
Question
I don`t have menu option in my Python. Why?
Ayodeji Oyeleye
Community Answer
You might be using a very old version of Python. Try downloading the latest version (Python 3.5.7).
-
Question
How do I write a program that will store the input after asking the user their name?
When the user is prompted for their name, the input is automatically stored in the variable 'name,' which was declared in the code asking for the input. The value stored in the variable name is the input prompt. When the user is prompted for their name, the input prompt is replaced with the string the user provides. In more simple terms: The user's name is stored when you ask the user for their name. Code: name = input("name: ").
-
Question
Will it work if I am using python 2.7?
Yes, but it would be better if you used the newer versions though, as the newer versions are easier to navigate and use. As well as this, most tutorials have been updated to only work with higher versions.
-
Question
It highlights "days" and says it's an invalid syntax. Why?
Make sure you used the same variable name. For example, if you used "day" as the variable name, then it must be used to manipulate or display the output.
-
Question
How do I get the Python Shell?
If you are using windows you will need to install a python IDE from the internet then just launch the IDE it will give you a python shell if you are a Linux user like me it's easy just type in terminal "python" and you'll get the shell i think this is the same for Mac computers.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Thanks for submitting a tip for review!
About This Article
Thanks to all authors for creating a page that has been read 223,377 times.
Is this article up to date?
How To Create A Program In Python
Source: https://www.wikihow.com/Create-a-Very-Simple-Program-in-Python
Posted by: turnerfreg1955.blogspot.com
0 Response to "How To Create A Program In Python"
Post a Comment