Python Forum

Full Version: while loop issue - stuck in the loop!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all!

I am very new to Python. I've been exposed to it for a couple of years, but just recently started working with it and studying it. I am attempting to write a simple calculator script, but the while loop in my code is getting stuck in a forever loop. I've tried several different iterations and my code looks correct based on what I have seen of the Python syntax. So I'm stuck. I could achieve the same thing using a for loop, I know, but I want to try to master the while loop in Python for future use. Here is my code:

#CALCULATOR

#Function: prompt user for what type of calculation to do as well as how many arguments will be used in calculation.
def GetCalcParametersInput():
	print "What operation? (Options: mult, div, add, sub):",
	calc_type = raw_input()
	print "How many arguments?:",
	calc_args = raw_input()
	
	arg_list = GetArgumentInput(calc_args)
	result = DoCalculation(calc_type, arg_list)
	print "ANSWER:", result

#Function: number of arguments to use in calculation passed in as variable....
#... prompts user to enter numbers based on how many arguments the calculation will need...
#... returns list of arguments. 
def GetArgumentInput(calc_args):
	arg_list = []
	arg_length = calc_args

	while True:
		num_arg = len(arg_list)
		print num_arg
		print arg_length
		if num_arg < arg_length:
			print "Argument_%d:" %(num_arg),
			arg_in = raw_input()
			arg_list.append(arg_in)
		else:
			break
		
	return arg_list

#Function: performs the calculation using specified parameters --- NOT YET IMPLEMENTED FULLY
def DoCalculation(calc_type, arg_list):
	#oper = GetOperation(calc_type)
	print "Will do a %s using %s." %(calc_type, arg_list)

#calls the initial function
GetCalcParametersInput()
And here is the resulting output from Powershell:
-------
What operation? (Options: mult, div, add, sub): mult
How many arguments?: 2
0
2
Argument_0: 5
1
2
Argument_1: 46
2
2
Argument_2: 465
3
2
Argument_3: 24
4
2
Argument_4: 6159
5
2
Argument_5:
6
2
Argument_6: 651651
7
2
Argument_7:
8
2
Argument_8: 651
9
2
Argument_9: 651
10
2
Argument_10: 351
11
2
Argument_11: 651
12
2
Argument_12: 351
13
2
Argument_13: 35
14
2
Argument_14: 16
15
2
Argument_15: 116
--------------

You will notice that the output asks for arguments well past the arg_list length of 2. In fact, under each Argument input, the first number printed is the appended arg_list length, while the second number printed is the arg_length, which is the initially passed calc_args. In the code, it SHOULD be breaking out of the loop when the arg_list length exceeds the arg_length, but the first number printed keeps increasing well past that point. So... there it is. Anyone got any ideas? I'm sure it's something small and simple, but I just can't see what it is!!! Thanks for any help! Cheers.

Eric
change:
calc_args = raw_input()
to
calc_args = int(raw_input())
There are other issues style, etc. but you can work on that
also, you never actually do the calculation
That was it!!!! Thank you so much Larz! It makes a TON of sense now.

As to your other comment, if you have time, what are some of your suggestions for style adjustment? Thank you so much for your reply!

Eric
You may be interested to see the cmd module.
(Aug-04-2018, 06:11 AM)EricMichel Wrote: [ -> ]what are some of your suggestions for style adjustment?

e.g. change function names from CammelCase to lowercase, with words separated by underscores as necessary to improve readability, use docstring instead of block comment to describe what function does...

check PEP8 - Style Guide for Python Code
you can also install pycodestyle and run your code against that
pip install pycodestyle
Docs: https://pycodestyle.readthedocs.io/en/latest/
@wavic @buran @Larz60+ Thank you all for the suggestions! I appreciate it! I will look into these and use them to help develop myself as a Python user. Thanks, again!

Eric