Problem with Python script

Hey guys,

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?

http://pastie.org/10920741#34-45

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

`

1 Like

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()

( or download: http://afiskon.ru/s/61/9f9dfbc175_age.py.txt )

Also please note that Python 2 end of life is scheduled for 2020. I would recommend to start investing time in Python 3.

2 Likes

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 :wink: - both are very similar in multiple ways.

Funny but some people still believe that 2020 is a far-far future. Actually it’s only 3.5 years in the future from now.

5 Likes

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.

3 Likes

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?

I apologize but I’m very new with python :slight_smile:

2 Likes

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 :slight_smile: Such things are explained like in first 10 pages of literally any such book.

3 Likes

I’ve checked your script, and from looking at it, you seem to have used variables as if they are pointers?..

In the main function, you’ve supplied:

name_errorcheck(name)
age_errorcheck(age)
Ins_errorcheck(lines)
hundredyears = int(thisyear+(100-age))

And the name_errorcheck will just return the value if it is okay.

return(name)

What you need to do, which infact you haven’t done, is actually set the variable with the output of the function:

name = name_errorcheck(name)

I’ve made this mistake before, it’s easy done :slight_smile:

Try this :wink:

2 Likes

Alright i’ll do that :slight_smile: thanks for your time…

1 Like