I’m an absolute programming noob and started to write a small script to train my Python.
Now I need your help!
The goal of the script is to show the user in what year he turns 100 .
The errorcheck isn’t to advanced yet but I’m planning to refine it. That’s not the problem.
The problem is that my global variables should be edited by the other three functions, whitch is not the case.
Do you have any suggestions what could be the problem?
Or maybe you have a complete solution which is short and works with error check?
How would this type of program be programmed professionally?
I’m open to other solutions and to learn how it is done right
As a rule of thumb never-never-ever use global variables for anything.
Here is a working script:
#!/usr/bin/env python2
import time
import os
def getname():
name = ""
while True:
name = raw_input("Please enter your name: ")
if name == "":
print("You have to enter a name")
continue
return(name)
def getage():
age = 0
while True:
age = int(raw_input("Please enter your age: "))
if age <= 0:
print("You have to enter e number for your age")
continue
return(age)
def getlinesnum():
lines = 0
while True:
lines = int(raw_input("How many lines: "))
if lines <= 0:
print("You have to enter a number for the lines")
continue
return(lines)
def main():
name = getname()
age = getage()
lines = getlinesnum()
# get current year
thisyear = int(time.strftime("%Y"))
# calculate the final year
hundredyears = int(thisyear+(100-age))
message = 'Hello %s, you will turn 100 years in: %s \n' % (name, hundredyears)
print(lines*message)
if __name__ == "__main__":
main()
Great catch by @afiskon. G’job mate. And yes he’s correct- I would start investing time in Python 3, won’t be around long. Although you could stay with Py 2 for now just to learn the basics - both are very similar in multiple ways.
Yeah, funny. For me it’s weird. Some days move so fast, yet sometimes a second feels like an hour. But yes it’s very-very short, yet for some reason people don’t realize it. I’m starting to think even when “py 2” ends in 2020 - people will still learn it and code with it.
Thanks man!
I am in fact using Python 3.5, which is probably the problem why raw_input doesn’t work for me.
I wanted to repeat the function until a correct value is entered.
How does the while loop you creeated get a true or fals value?
And what does the (if name == “main”: ) above the main function call do?
It always gets True. When user enters something that passes a validation the whole procedure return-s right from the while cycle.
And what does the (if __name__ == "__main__": ) above the main function call do?
It .py file is called as a script (not used as a library) __name__ global variable equals "__main__". It’s a common idiom in Python world. This way your script could be used both as a script and as I library.
As a side note - don’t get me wrong but you could really use a good book about Python basics Such things are explained like in first 10 pages of literally any such book.