简体   繁体   中英

Python: How can I use a for loop to execute my code 5 times?

I am new to python and working on basic functions and conditionals.

Here's an example of 1 function in my code:

def oddEven(oe):
    if oe % 2 == 0:
        print("number is even")
    else:
        print("number is odd")

There are 3 total functions in my code (omitted because unnecessary). The first two pass the value of the integer named first , and the last function takes two integer parameters and pass both first and second and checks the result.

Here is how I'm recalling these and checking my code

first = 30
second = 60
oddEven(first)
minutes(first)
relation(first, second)

Instructions say:

Run the program five times using the provided first values and your chosen second values, and check your results. You can alternatively run the program once and use a for loop to execute your code five times.

If the values were for example 23, 33, 14, 31 I could just do:

oddEven(23)
oddEven(33)
oddEven(14)
oddEven(31)

Right? And then do the same for the second function and the third. How could I make a for loop for this?

This is simple way to do this :

list_values = [22,33,14,31]

for value in list_Values:
   oddEven(value)

You're right, you use a for loop in order to do that. The previous answers may have been able to iterate through the list for both the evenOdd and minutes functions, but they didn't handle the case where you compare two consecutive values for your relation function.

Create a list of 5 numbers and iterate through it. Handling the case where you're comparing two consecutive numbers against each other can be done with a simple if statement that stops executing once you've reached the second to the last element in the list.

If you're not familiar with lists yet, think of them as 'containers' for your data; where you can systematically store and access each piece one at a time.

What's happening in the code is the for loop iterates through the list one element at a time. The function range(n) takes in a number n, for an argument, and then creates a 'range' of numbers from 0 to n - 1.(Ex. range(2) will iterate through the for loop 2 times giving ia value of 0 on the first loop and 1 on the second.

An element from a list can be accessed with this notation: list[i], where i is a number that starts from 0 (therefore accessing the first element of the list) and ranges all the way up to the (length of the list - 1). So to access the third element in a list would be done like so: list[2].

The code below is pretty modular so you can add any amount of numbers to the numbers[] list and it should work, given that there is more then 1 number in the list. It's a good practice to get in the habit of making your code as modular as possible, instead of hard coding in constants. This is done by passing the (length of the list) to the range(), which allows the for loop to iterate over any size list.

If you add only one number to the list ( ex. numbers = [42]), the only thing that would happen is that the code inside the if statement will not execute, as it requires there to be more than 1 number in the list. The oddEven and minute function should still work though. Go ahead and give that a try! Try adding more than 5 numbers to the list too.

 numbers = [23, 33, 14, 21, 42]
 
 for i in range(len(numbers)):
     first = numbers[i]  
     oddEven(first)
     minutes(first)
     if i < (len(numbers) - 1):
         second = numbers[i + 1]
         relation(first, second)

    

 
  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM