Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How can I iterate this list over a loop? I am a beginner and python is my first language

$
0
0

Zybooks Instructions

List data_list contains integers read from input, representing a sequence of data values. For each index i of data_list from 1 through the second-to-last index:

The element at index i is a hunch if the element is greater than both the preceding element and the following element.If the element at index i is a hunch, then output 'Hunch: ', followed by the preceding element, the current element, and the following element, separating each element by a space.

Example

Ex: If the input is 12 14 72 52, then the output is:

  • Sequence: [12, 14, 72, 52]
  • Hunch: 14 72 52

Zybooks code

tokens = input().split()data_list = []for token in tokens:    data_list.append(int(token))print(f'Sequence: {data_list}')

My Code to complete zybooks code

for i, data in enumerate(data_list[1: -2]):    if data_list[i] > data_list[i - 1] and data_list[i] > data_list[i + 1]:        print(f"Hunch: {data_list[i - 1]} {data_list[i]} {data_list[i + 1]}")

The output with 12, 14, 72, 52##

  • Sequence: [12, 14, 72, 52]

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>