0

I am relatively new to python and I have searched the web for an answer but I can't find one.

The program below asks the user for a number of inputs, and then asks them to input a list of integers of length equal to that of the number of inputs. Then the program iterates through the list, and if the number is less than the ToyValue, and is less than the next item in the list the variable ToyValue increments by one.

NoOfToys=0
ToyValue=0
NumOfTimes=int(input("Please enter No of inputs"))
NumberList=input("Please enter Input")
NumberList=NumberList.split(" ")
print(NumberList)
for i in NumberList:
    if int(i)>ToyValue:
        ToyValue=int(i)
    elif int(i)<ToyValue:
        if int(i)<int(i[i+1]):
            NoOfToys=NoOfVallys+1
            ToyValue=int(i[i+1])
    else:
        pass
print(NoOfVallys)

Here is an example of some data and the expected output.

#Inputs
8
4 6 8 2 8 4 7 2
#Output
2

I believe I am having trouble with the line i[i+1], as I cannot get the next item in the list

I have looked at the command next() yet I don't think that it helps me in this situation.

Any help is appreciated!

3
  • 1
    for i in NumberList: iterates through the "items" of the list. this means that i contains the values of the list, not indexes. thus i[i+1] doesn't work. Check out enumerate if you want to work on indexes. Commented Feb 13, 2019 at 18:26
  • NoOfVallys it is not defined, could you define please Commented Feb 13, 2019 at 18:33
  • Note that you can just convert the whole list to integers at the start, instead of doing it every time you access a value: NumberList = [int(num) for num in NumberList.split(" ")]. Commented Feb 13, 2019 at 18:34

1 Answer 1

3

You're getting mixed up between the items in the list and index values into the items. What you need to do is iterate over a range so that you're dealing solidly with index values:

NoOfToys = 0
ToyValue = 0
NumOfTimes = int(input("Please enter No of inputs"))
NumberList = input("Please enter Input")
NumberList = NumberList.split(" ")
print(NumberList)
for i in range(0, len(NumberList)):
    value = int(NumberList[i])
    if value > ToyValue:
        ToyValue = value
    elif value < ToyValue:
        if (i + 1) < len(NumberList) and value < int(NumberList[i + 1]):
            NoOfToys = NoOfVallys + 1
            ToyValue =  int(NumberList[i + 1])
    else:
        pass
print(NoOfVallys)

You have to be careful at the end of the list, when there is no "next item". Note the extra check on the second "if" that allows for this.

A few other observations:

  • You aren't using the NumOfTimes input
  • Your logic is not right regarding NoOfVallys and NoOfToys as NoOfVallys is never set to anything and NoOfToys is never used
  • For proper Python coding style, you should be using identifiers that start with lowercase letters
  • The "else: pass" part of your code is unnecessary
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.