Python Forum
How give an for loop an maximum number, help pleace ?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How give an for loop an maximum number, help pleace ?
#1
Hey Python Programmers...

I try to learn python and i use 'python 3'. But i try to create an 'for loop'
with an maximum number. But it did't work. Can anyone help me ?...

# Importing:
import math

# Call Variables:
press = str(input("Press 'A' to start the program: "))

arrayX = int(input("Give 'X' Number: "))
arrayY = int(input("Give 'Y' Number: "))
arrayZ = int(input("Give 'Z' Number: "))

arrayStart = int(input("Start Number: "))
arrayRange = math.floor(int(input("Give an number of 'Floor': ")))


# Write 'for loop':
def program():
    for arrayRange in range(arrayStart, arrayX):
        for arrayRange in range(arrayStart, arrayY):
            for arrayRange in range(arrayStart, arrayZ):
                print("Array is Called in ", arrayRange, arrayX, arrayY, arrayZ)
                
                if(arrayRange >= 100):
                    arrayRange += 0



# Than... press 'A' to start this for loop:
if(press == "A"):
    press = "A"
    program()
This is an example what i like to try, an code like writting above... Can enyone help me
with how i can give an 'for loop' an max number ?...

Thanks, Jamie.
Reply
#2
I don't know what you are trying to do. I don't understand "'for loop' an max number." Please explain exactly how the output you are getting is not the output you are expecting.

That said, I see a lot of issues with your code. Mostly you are doing a lot of stuff you don't need to do. The output of input is a string, so you don't need to make it a string again with str() on line 5. The math.floor function returns the largest integer <= the input. There is no reason to use it on an integer on line 12. arrayRange += 0 adds zero to arrayRange. That is, it does nothing. On line 28 you confirm press is 'A', so there is no reason to set it to 'A' on line 29.

The main problem I see is that you use the variable name arrayRange in four different places. Once to get user input, and three times as three different loop variable. Each loop should have it's own variable, distinct from other variables. Your loops should be something like this:

for loop_variable in range(start, finish):
The above loop will take the values from start to finish - 1 and put them in the variable loop_variable. Again, each loop should have a different variable name in the place of loop_variable.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Aug-08-2017, 01:55 PM)ichabod801 Wrote: I don't know what you are trying to do. I don't understand "'for loop' an max number." Please explain exactly how the output you are getting is not the output you are expecting.

That said, I see a lot of issues with your code. Mostly you are doing a lot of stuff you don't need to do. The output of input is a string, so you don't need to make it a string again with str() on line 5. The math.floor function returns the largest integer <= the input. There is no reason to use it on an integer on line 12. arrayRange += 0 adds zero to arrayRange. That is, it does nothing. On line 28 you confirm press is 'A', so there is no reason to set it to 'A' on line 29.

The main problem I see is that you use the variable name arrayRange in four different places. Once to get user input, and three times as three different loop variable. Each loop should have it's own variable, distinct from other variables. Your loops should be something like this:

for loop_variable in range(start, finish):
The above loop will take the values from start to finish - 1 and put them in the variable loop_variable. Again, each loop should have a different variable name in the place of loop_variable.

OK... sorry for my too complex code... Now I get
an other way to give it an max...

An code like this:
question = str(input("Your Name: "))

numbs = range(int(Input("Range Number")))

for numbs in numbs:
    print("Your name is ", question)

(Aug-08-2017, 07:00 PM)JamieVanCadsand Wrote:
(Aug-08-2017, 01:55 PM)ichabod801 Wrote: I don't know what you are trying to do. I don't understand "'for loop' an max number." Please explain exactly how the output you are getting is not the output you are expecting.

That said, I see a lot of issues with your code. Mostly you are doing a lot of stuff you don't need to do. The output of input is a string, so you don't need to make it a string again with str() on line 5. The math.floor function returns the largest integer <= the input. There is no reason to use it on an integer on line 12. arrayRange += 0 adds zero to arrayRange. That is, it does nothing. On line 28 you confirm press is 'A', so there is no reason to set it to 'A' on line 29.

The main problem I see is that you use the variable name arrayRange in four different places. Once to get user input, and three times as three different loop variable. Each loop should have it's own variable, distinct from other variables. Your loops should be something like this:

for loop_variable in range(start, finish):
The above loop will take the values from start to finish - 1 and put them in the variable loop_variable. Again, each loop should have a different variable name in the place of loop_variable.

OK... sorry for my too complex code... Now I get
an other way to give it an max...

An code like this:
question = str(input("Your Name: "))

numbs = range(int(Input("Range Number")))

for numbs in numbs:
    print("Your name is ", question)

The problem with my code is fixed....

Now I do for numbs in numbs and tha print my string...
But thanks....

Jamie.


Reply
#4
(Aug-08-2017, 07:00 PM)JamieVanCadsand Wrote:
(Aug-08-2017, 01:55 PM)ichabod801 Wrote: I don't know what you are trying to do. I don't understand "'for loop' an max number." Please explain exactly how the output you are getting is not the output you are expecting.

That said, I see a lot of issues with your code. Mostly you are doing a lot of stuff you don't need to do. The output of input is a string, so you don't need to make it a string again with str() on line 5. The math.floor function returns the largest integer <= the input. There is no reason to use it on an integer on line 12. arrayRange += 0 adds zero to arrayRange. That is, it does nothing. On line 28 you confirm press is 'A', so there is no reason to set it to 'A' on line 29.

The main problem I see is that you use the variable name arrayRange in four different places. Once to get user input, and three times as three different loop variable. Each loop should have it's own variable, distinct from other variables. Your loops should be something like this:

for loop_variable in range(start, finish):
The above loop will take the values from start to finish - 1 and put them in the variable loop_variable. Again, each loop should have a different variable name in the place of loop_variable.

OK... sorry for my too complex code... Now I get
an other way to give it an max...

An code like this:
question = str(input("Your Name: "))

numbs = range(int(Input("Range Number")))

for numbs in numbs:
    print("Your name is ", question)

(Aug-08-2017, 07:00 PM)JamieVanCadsand Wrote: OK... sorry for my too complex code... Now I get
an other way to give it an max...

An code like this:
question = str(input("Your Name: "))

numbs = range(int(Input("Range Number")))

for numbs in numbs:
    print("Your name is ", question)

The problem with my code is fixed....

Now I do for numbs in numbs and tha print my string...
But thanks....

Jamie.


Oops... Sorry for my empty code...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Give visit number ( or counter) within the same time frame balthordu 1 941 Jun-27-2022, 09:51 AM
Last Post: Larz60+
  Give a number for Variable quest 2 1,506 Jan-31-2022, 08:35 AM
Last Post: ibreeden
  How to verify the give number is valid Mekala 3 2,396 May-16-2020, 02:40 PM
Last Post: anbu23
  number repeating twice in loop JonnyEnglish 3 3,307 Nov-24-2019, 09:23 AM
Last Post: ThomasL
  Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no. allusernametaken 4 2,894 Nov-17-2019, 02:56 AM
Last Post: allusernametaken
  Setting maximum value of Range() in For loop pmt 4 2,593 Aug-04-2019, 02:38 PM
Last Post: pmt

Forum Jump:

User Panel Messages

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