Home > OS >  trying to perform specific operation on specific number in a list in python
trying to perform specific operation on specific number in a list in python

Time:12-07

I am trying to solve this question:

Write a Python code to accept five number from the user to create a List. Traverse the list and print double the value of even numbers and triple the value of odd numbers.

I was successful appending the numbers to the right indexes but my if and for loops are screwing me over.

My code (doesn't work well):

num1 = int(input("Enter Number 1: "))
num2 = int(input("Enter Number 2: "))
num3 = int(input("Enter Number 3: "))
num4 = int(input("Enter Number 4: "))
num5 = int(input("Enter Number 5: "))

num_list = []
num_list.append(num1)
num_list.append(num2)
num_list.append(num3)
num_list.append(num4)
num_list.append(num5)

for i in num_list:
    if i % 2 == 0:
        i *= 2
        num_list.append(i)
    else:
        i *= 3
        num_list.append(i)
print(num_list)

CodePudding user response:

Your assignment requires you to print the computed values. Currently you are appending to the list you are iterating over, resulting in an infinite loop.

Change num_list.append(i) to print(i).

CodePudding user response:

Traverse the list and print double the value of even numbers and triple the value of odd numbers.

You can literally translate this English sentence to Python:

for i in num_list:     # traverse the list
    if i % 0 == 0:
        print(2 * i)   # print double the value of even numbers
    else:
        print(3 * i)   # print triple the value of odd numbers

CodePudding user response:

x = 0 # initiate the while iteration var to stop the while loop`
num_list = [] # initiate empty list to append the input from user

while x < 5:`
    numb = int(input("enter a number:")) # input var
    num_list.append(numb)
    x  = 1 # increment to ensure the while loop doesn't go on infinitely

    print(num_list) # print this to ensure the first block did as required

for i in num_list:
        if i % 2==0: # first if statement for even numbers`
            `print(i*2)  #you can change to append however your problem said print
`        elif i % 2 != 0: # check for odd numbers in the list
            print(i * 3)

CodePudding user response:

If you want to take input five times into a list, why in the world are you creating five variables first ? Just append directly. Then iterate over that list and print doubles and triples:

inlist = []
for i in range(5): # how many times the user should be asked for input
    instr = input("Enter number: ") # take input 
    innum = int(instr) # convert to number
    inlist.append(innum)

for num in inlist:
    if num % 2 == 0: # even
        print(num * 2)
    else: # odd
        print(num * 3)

If you want it shorter:

inlist = []
for i in range(5): # how many times the user should be asked for input
    instr = inlist.append(int(input("Enter number: "))) 

for num in inlist:
    if num % 2 == 0: # even
        print(num * 2)
    else: # odd
        print(num * 3)

This is not suitable for your assignment, only for yourself

That's how one would do it in a real application:

inlist = [int(input("Enter number: ")) for i in range(5)]

for num in inlist:
    print(num * [2, 3][num % 2])

Or even:

inlist = [int(input("Enter number: ")) for i in range(5) * [2, 3][num % 2]]
  • Related