Python Forum
while loop issue - stuck in the loop!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop issue - stuck in the loop!
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
You may be interested to see the cmd module.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
you can also install pycodestyle and run your code against that
pip install pycodestyle
Docs: https://pycodestyle.readthedocs.io/en/latest/
Reply
#7
@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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through all files in a directory? Winfried 10 304 Yesterday, 07:38 PM
Last Post: FortuneCoins
  for loop not executing Abendrot47 2 206 Apr-09-2024, 07:14 PM
Last Post: deanhystad
  Re Try loop for "net use..." failures tester_V 10 605 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 799 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 488 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 365 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 405 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Variable definitions inside loop / could be better? gugarciap 2 440 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 620 Dec-28-2023, 05:14 PM
Last Post: trix
  Need an alternative to brute force optimization loop jmbonni 5 1,179 Dec-07-2023, 12:28 PM
Last Post: RockBlok

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020