Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

New Member & Python Question


  • Please log in to reply

#1
loxs

loxs

    Member

  • Member
  • PipPip
  • 24 posts
Hello all....... This is my first post and I thought I would start by asking a relatively simple question. I'm going through a few online guides, trying to learn how to do a few things with python. I'm not sure why, I guess it's more of a hobby than anything. I just dig knowledge. My question is:
I wrote a simple program that divides numbers with the choice of having an output of a sum with a remainder or with the sum rounded off. This is how I done it: (the important part of it anyway)

def remain(a,b):
	return a/b,a%b

def round(a,b):
	a = a/b
	return a

loxs = 3

while loxs != 0:
	loxs = input(">*> ")
	if loxs == 1:
		x=input("please choose the first number ")
		y=input("please choose the second number ")
		print remain(x,y)
	elif loxs == 2:
		x=input("please choose the first number ")
		y=input("please choose the second number ")
		print round(x,y)
	elif loxs > 2:
		print "Please choose 0-2

I had to write the x=input and y=input twice,once for each "if" statement which, in my mind, would not be very efficient in a larger program.

I could:

def user():
	global x,y
	x=input("Please choose the first number ")
	y=input("Please choose the second number ")

and then:

loxs = 3
while loxs != 0:
	loxs = input(">*> ")
	if loxs == 1:
		user()
		print remain(x,y)
	elif loxs == 2:
		user()
		print round(x,y)
	elif loxs > 2:
		print "Please choose 0-2"

BUT, from what I have read, the "global" function can and will get you in trouble with larger programs.
Is this correct or am I way off base here?
Any suggestions on a better/safer way of doing it?

Thanks..........................................................................
..Loxs
  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP