Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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