[Help] For Statement

A forum for general discussion of the Python programming language.

[Help] For Statement

Postby YaFedImYaEatIm » Mon Feb 03, 2014 5:08 am

Hello

I wish to keep the for statement for adding towards the variable created when the for statement was created part of my code is
Code: Select all
def inputheights(x):
    for y in range(0, int(x)):
        input_height = input('Please enter the height of Student ' + str(y + 1) + "\n")
        if str.isdigit(input_height):
            heights.append(input_height)
            print('success')
        else:
            y = y - 1
            print('That is not a valid height')

Is there any possible way to do this or I will have to consider using a while statement instead

Yours Sincerely

YaFedImYaEatIm (Recently transferred from Java)
Last edited by micseydel on Mon Feb 03, 2014 5:42 am, edited 1 time in total.
Reason: Locked OP.
YaFedImYaEatIm
 
Posts: 2
Joined: Mon Feb 03, 2014 5:04 am

Re: [Help] For Statement

Postby micseydel » Mon Feb 03, 2014 6:58 am

I'm really not clear here on what it is you want. Could you tell us what the input and output is for your function for a relevant input value? We can also probably convert a while loop that does what you want to the correct for loop. Python has a lot of facilities to be able to use for loops instead of while loops.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1371
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: [Help] For Statement

Postby YaFedImYaEatIm » Wed Feb 05, 2014 10:40 pm

Thank you for the reply. The code above was meant to use the for statement to loop the heights of the students and store them into and check after the input that it is a digit that was input and if it isn't then it decreases the delared value shown as (y = y - 1) thus it would show the same student number again. If that makes sense I'm not sure if I explained it properly
YaFedImYaEatIm
 
Posts: 2
Joined: Mon Feb 03, 2014 5:04 am

Re: [Help] For Statement

Postby stranac » Wed Feb 05, 2014 11:11 pm

Using a while loop inside the for loop is probably the way to go.
Something like this:
Code: Select all
for i in range(x):
    while True:
        input_height = input('Please enter the height of Student %s \n' % i)
        if input_height.isdigit():
            heights.append(input_height)
            break
        else:
            print('That is not a valid height')


Just a few extra notes:
  • Appending to a global list like that is rarely a good idea. Better to create a list inside the function and return it.
  • Making this a generator would probably be a good idea.
  • You're using the string methods wrong(check my code)
  • range() starts at 0 by default
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Discussions

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests