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
  Returning data on button click by buttons created by a loop bradells 3 416 Apr-23-2025, 03:01 PM
Last Post: Pedroski55
  in c# create a loop counting from 0 to 5, consecutively Frankd 19 2,315 Apr-01-2025, 12:46 PM
Last Post: Frankd
  really new to python want to know how to do a loop pentopdmj 6 1,645 Mar-09-2025, 12:59 PM
Last Post: snippsat
  knowing for loop position in a list medic5678 4 722 Jan-31-2025, 04:19 PM
Last Post: perfringo
  Run this once instead of a loop, do I need the 'calibration' steps? duckredbeard 2 749 Jan-28-2025, 04:55 PM
Last Post: duckredbeard
  Error loop with Chatgpt sportak12 0 528 Jan-14-2025, 12:04 PM
Last Post: sportak12
  How to convert while loop to for loop in my code? tatahuft 4 860 Dec-21-2024, 07:59 AM
Last Post: snippsat
  How to get keep information in a loop ginod 4 916 Dec-11-2024, 01:32 AM
Last Post: deanhystad
  Regarding The For Loop Hudjefa 5 1,512 Nov-15-2024, 01:02 PM
Last Post: deanhystad
  For Loop beginner agoldav 2 752 Nov-05-2024, 12:51 AM
Last Post: agoldav

Forum Jump:

User Panel Messages

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