The iterator is not iterable because the object is an integer and the reason is unknown

Recently, I embarked on the journey of programming and encountered a code that involved finding the smallest value among 10 integer inputs. Here is my code:

x, y = 0, 0 

while x < 10:
    n = int(input())
    y += n
    x += 1
print(y)

s = min(y)
print(s)

Despite my efforts, an issue arose when trying to execute 's = min(y)' which resulted in a TypeError: 'int' object is not iterable. How can I rectify this problem?

Answer №1

When you add one int to another, the result is a single int, not a pair of two separate integers. If you try to use the min function on just an int, it will throw an error because it expects an iterable such as a list.

Here is the corrected way to achieve this:

x, y = 0, []

while x < 10:
    n = int(input())
    y.append(n)
    x += 1
print(y)

Now that y is a list of integers, you can find the smallest value in it using the min function like this:

s = min(y)
print(s)

An alternative method to create y more efficiently is by using a list comprehension along with a range:

y = [int(input()) for _ in range(10)]
print(min(y))

Answer №2

Don't expect Python to return the minimum number of one number when using the min() function, it doesn't work that way.

Instead, you can find the minimum of a list of numbers by using min([5,2]) -> 2. So make sure to write:

x = min([z])

This will assign the value of z to x.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking to retrieve the genre information from the IMDb movie page

I'm tackling the challenge of extracting a list of genres from any movie page on IMDb. For example: Movie page: List of Genres: [Crime, Drama, Mystery, Thriller] Despite my attempts with Beautiful Soup, I haven't been able to pinpoint the exa ...

Azure Active Directory: Access Token Response Body Does Not Contain "Scope" Property

I am currently developing a Python script to modify an Excel spreadsheet stored in SharePoint within our Office 365 for Business environment. To achieve this, I am utilizing the Microsoft Graph API and have registered my Python application with Microsoft A ...

Utilizing Cross-Validation post feature transformation: A comprehensive guide

My dataset contains a mix of categorical and non-categorical values. To handle this, I used OneHotEncoder for the categorical values and StandardScaler for the continuous values. transformerVectoriser = ColumnTransformer(transformers=[('Vector Cat&apo ...

How can a multidimensional array be stored as bit data?

Currently, I am faced with a large numpy matrix that was generated using the following code snippet: np.full(np.repeat(2, 10), 1,dtype='int8') The shape of this matrix is as follows: (2, 2, 2, 2, 2, 2, 2, 2, 2, 2) However, all the values in t ...

How can I learn to create a Python script that can solve this issue?

I want to write python code that generates the following outputs. The program should have prefixes of "JKLMNOPQ" and suffixes of "ack": Jack Kack Lack Mack Nack Oack Pack Quack Thank you very much. ...

Can Selenium accurately find elements using partially matched classes?

As I begin my journey with Selenium, I am faced with the task of locating the Next button on the CNN website. If it is not the last page, I need to click on it; otherwise, I must end the program. The HTML code for an enabled button looks like this: <di ...

python create dictionary using tuples as composite keys

I am currently working on converting a list of tuples to a composite-key dictionary in order to enable custom sorting by either first or last name: def phone(first,last,number): directory = dict() while True: first = input('Please ent ...

Learn how to implement hooks that run before committing changes and others that run before pushing code using pre-commit

Some hooks can take a while to run, and I prefer running them before pushing rather than before each individual commit (pylint being one such example). I've come across the following resources: Inquiry: Using Hooks at Different Stages mesos-commits ...

Is there a way to load JSON data into an OrderedDict structure?

Is it possible to use an OrderedDict as an output in json.dump? I know it can be used as an input, but I'm curious about maintaining key order when loading into an OrderedDict. If it's not directly possible, are there any alternative solutions o ...

Tips for providing a dynamic value in an XPATH element with Python Selenium

game = "Chess" driver.find_element(By.XPATH, "//div[@title = {game}]").click() I'm having trouble making my code function properly. Can you provide guidance on how to insert a dynamic variable into the XPATH? ...

How can I create a custom filter for model choice field selections in a user-specific way using Django?

This is the form I've created: class RecipeForm(forms.Form): def __init__(self, *args, **kwargs): self.user = kwargs.pop('user', None) super(RecipeForm, self).__init__(*args, **kwargs) Recipebase_id = forms.ModelCho ...

Warning: The variable "color" has been assigned before being declared as a global variable in Python

Looking at the code snippet below, there is an interesting message: "SyntaxWarning: name 'color' is assigned to before global declaration global color" Despite my initial declaration of global color before assigning it a value, this warning is ...

Leveraging Selenium for extracting data from a webpage containing JavaScript

I am trying to extract data from a Google Scholar page that has a 'show more' button. After researching, I found out that this page is not in HTML format but rather in JavaScript. There are different methods to scrape such pages and I attempted t ...

Issues with deleting dictionaries from a list using Python

I have encountered a puzzling issue in my code. There are certain pieces of logic where keys/values or dictionaries get removed under specific conditions. However, when these objects are added to a top-level dictionary and converted into a JSON object, the ...

Adjusting the size of a dialog box using PyQt4

Check out this code sample: import sys from PyQt4.QtGui import (QApplication, QHBoxLayout, QVBoxLayout, QDialog, QFrame, QPushButton, QComboBox) class Widget(QDialog): def __init__(self, parent=None): ...

Pandas: A guide to generating a count for every repeated row in a dataframe

After creating a dataframe consisting of only duplicated rows using the duplicated() method, I have a question that may be simple. I want to include a count column on the right side where each row indicates how many times it appears in the duplicated dataf ...

Displaying the contents of every file within the given directory

I'm facing a challenge where I need to access a directory and display the content of all files within it. for fn in os.listdir('Z:/HAR_File_Generator/HARS/job_search'): print(fn) Although this code successfully prints out the names of ...

What is the best way to analyze the similarities and differences between the nodes and edges within two

There are graphs of Berlin from January 1, 2020 and January 1, 2021. How can I compare the changes in edges and nodes between the two maps? import osmnx as ox import pandas as pd import networkx as nx import matplotlib.pyplot as plt from itertools import ...

Looking to access nasdaq.com using both Python and Node.js

Having an issue with a get request for nasdaq.com. Attempting to scrape data with nodejs, but only receiving 'ECONNRESET' after trying multiple configurations. Python, on the other hand, works flawlessly. Currently using a workaround by fetching ...

Having trouble using the Python append method for axis=1 in a 2D array?

#Issue with Python's append function when axis=1 in 2D array. import numpy as np arr = np.array([[11, 15, 10, 6], [10, 14, 11, 5], [12, 17, 12, 8], [15, 18, 14, 9]]) print(arr) ...