Add integers to a list? - python

I am trying to ask the user for several values for temperatures of several month by the "monthtemp" input and then add all the values to a list and in the end print out the average for the whole year.
count = 1
loops = int(input("How many years?: "))
listofmonthtemperatures= list()
while count < loops:
for i in range(1,loops+1):
count += 1
year = input("Which is the " + str(i) + ": year?: ")
for j in range (1,13):
monthtemp= int(input("Month " + str(j) + ": "))
listofmonthtemperatures.append(monthtemp)
total= sum(listofmonthtemperatures)
averagetempforyear= total / 12
print("The average temperature for", year, "is", averagetempforyear)
But I get the message year is not defined, but haven't I defined it as whatever the user inputs? And second, will this work? Why can't append simply append the value to listofmonthtemperatures?

I copy/pasted your code and it worked just fine. Maybe this was a typo but your for loops weren't properly indented. I'm running 3.4.3 btw.
count = 1
loops = int(input("How many years?: "))
listofmonthtemperatures= list()
while count < loops:
for i in range(1,loops+1):
count += 1
year = input("Which is the " + str(i) + ": year?: ")
for j in range (1,13):
monthtemp= int(input("Month " + str(j) + ": "))
listofmonthtemperatures.append(monthtemp)
total= sum(listofmonthtemperatures)
averagetempforyear= total / 12
print("The average temperature for", year, "is", averagetempforyear)
Above is the working code with the proper indentation.

Related

adding to a string based on the number of characters

the problem says to print "that's a really really ... big number" with one "really" for every extra digit that the number has (so 15 would be "that's a really big number", 150 would be "that's a really really big number", 1500 would be "that's a really really really big number", and so on.)
the input is an integer, and the only requirements listed are that the code should run correctly with any integer, should use a while loop to keep dividing the number by 10 and should use += to add onto the end of a string
x = input(("input an integer: "))
count = len(x)
y = int(x / 10)
countx = count - 1
print("that's a " + count("really") + " big number")
i don't really know what i did, but i can tell it's not correct
Try this one. Uses while loop to divide number by 10 and += to add onto the string. You will need to take a string variable and then append to it as count increases. Loop will run until number >= 10 as conditions you mentioned.
x = int(input(("input an integer: ")))
str=""
while x>=10:
x=x//10
str+="really "
print("that's a " + str + "big number")
Added the while loop as you stated in your question
strVar = ""
y = 0
length = 0
cnt = 0
finished = False
num = input("Type a number: ")
while not finished:
y = int(num)//10
length = int(len(str(y)))
if length <= cnt:
finished = True
else:
strVar += " really"
cnt += 1
print("That's a" + strVar + " big number!")
Give this a try. It finds how many trailing zeroes there are in a given number and based on that creates a certain amount of really's.
x = input(("input an integer: "))
count = int(len(x) - len(x.rstrip('0')))
if count == 0:count = 1
really = "really "*count
print(f"that's a {really} big number")
Try this
x = int(input("enter an integer"))
print("that's a " + 'really '*(len(str(x))-1) + " big number")
If you want to correct your function use this
x = input(("input an integer: "))
count = len(x)
y = int(x) / 10
countx = count - 1
print("that's a " + countx*" really" + " big number")

TypeError: 'int' object is not iterable (new beginner)

This is my Task:
Require the user to enter their name, with only a certain name being able to trigger the loop.
Print out the number of tries it took the user before inputting the correct number.
Add a conditional statement that will cause the user to exit the program without giving the average of the numbers entered if they enter a certain input
numbers = []
number = 0
count = 0
total = 0
name = 0
while number >= 0:
number = int(raw_input("Please enter any number: \n"))
if number == -1:
break
numbers.append(number)
avg = float(sum(numbers)) / len(numbers)
print "The average of the numbers you entered is " + str(avg) + "!"
while name >= 0:
name = int(raw_input("Please enter the number of characters your name contains: \n"))
count += 1
total += count
if name == 6:
break
tries = sum(total)
print tries
tries = sum(total)... sum takes an iterable whereas total is an int hence the error.
If total = [ ]
and you can append count values in total that would be fine.
so total.append(count)
would create list then you can use sum(total) on it
but here you can simply use print total or count both are same.
numbers = []
number = 0
count = 0
total = 0
name = 0
while number >= 0:
number = int(raw_input("Please enter any number: \n"))
if number == -1:
break
numbers.append(number)
avg = float(sum(numbers)) / len(numbers)
print "The average of the numbers you entered is " + str(avg) + "!"
while name >= 0:
name = int(raw_input("Please enter the number of characters your name contains: \n"))
count += 1
total += count
if name == 6:
break
tries = total
print tries
and you try to reduce your code.

While loop returned nothing at all and removing duplicates of min max

1) upon entering input >> 1 2 3 4 5 6 7
the result returned nothing. Must be the while loop i supposed?
2) Also for input such as >> 1 1 1 5 5 7 7 7 7
how do i remove duplicate of 1 and 7? ; meaning duplicate of min and max.
I plan to average the number input by removing duplicate of min and max.
Do i combine max() min() function with list(set(x)) or is there another way round?
New to python here. WHILE permitted only. Do not suggest For
even_sum, odd_sum = 0,0
evencount, oddcount = 0,0
count=0
n=0
s = raw_input("Please Input a series of numbers")
numbers = map(int, s.split())
while count<numbers:
if numbers[n]%2==0:
evencount = evencount +1# len(numbers)
even_sum += num
count=count+1
n=n+1
else:
oddcount = oddcount+1#len(numbers)
odd_sum += num
count=count+1
n=n+1
max123 = max(numbers)
min123 = min(numbers)
difference = max123 - min123
print numbers
numbers.remove(max(numbers))
numbers.remove(min(numbers))
average = sum(numbers)/float(len(numbers))
print "The summation of even and odd numbers are " + str(even_sum) + " and " + str(odd_sum)
print "The difference between biggest and smallest number is " + str(difference)
print "The count of even numbers and odd numbers are " + str(evencount) + " & " + str(oddcount)
print average
This is wrong:
while count<numbers:
You are comparing a number to a list. That is valid, but it does not do what you might expect. count<numbers is always true, so you are stuck in an infinite loop. Try it:
>>> 1000000 < [1, 2]
True
What you want to do instead is iterate through all numbers.
for number in numbers:
if number % 2 == 0:
...
You don't need count and you don't need n either.
Also, the else: should be indented, otherwise it will never be executed with this code.
Handicap mode (without for)
n = 0
odd_count = 0
odd_sum = 0
while n < len(numbers):
number = numbers[n]
if number % 2:
odd_count += 1
odd_sum += number
n += 1
# "even" values can be calculated from odds and totals
#This is modified code i did mark (#error) where your program didn't work
even_sum, odd_sum = 0,0
evencount, oddcount = 0,0
count=0
n=0
s = raw_input("Please Input a series of numbers")
numbers = map(int, s.split())
print (numbers)
while count<len(numbers): #error thats a list you have to convert that in to int
if numbers[n]%2==0:
evencount = evencount +1# len(numbers)
even_sum += numbers[n] #error index and variable not defined
count=count+1
n=n+1
else: #indented error
oddcount = oddcount+1#len(numbers)
odd_sum += numbers[n] #error index and variable not defined
count=count+1
n=n+1
max123 = max(numbers)
min123 = min(numbers)
difference = max123 - min123
numbers.remove(max(numbers))
numbers.remove(min(numbers))
average = sum(numbers)/float(len(numbers))
print "The summation of even and odd numbers are " + str(even_sum) + " and " + str(odd_sum)
print "The difference between biggest and smallest number is " + str(difference)
print "The count of even numbers and odd numbers are " + str(evencount) + " & " + str(oddcount)

Python 3.4.2 | Loop input until set amount

Learning the basics of python and I am running across a problem. I'm sure it is a simple fix. I'm trying to get my program to get 20 different inputs before calculating the min, max, etc.
def main():
number = valueInput()
display(number)
def valueInput():
print("Please enter 20 random numbers")
values = []
for i in range(20):
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
values.append(value1)
return values
def display(number):
print("The lowest number is:", min(number))
print("The highest number is:", max(number))
print("The sum of the numbers is:", sum(number))
print("The average number is:", sum(number)/len(number))
main()
I can get it to work by repeating this line:
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
to the 20th number but there must be a way that makes it shorter and cleaner. Again I am a beginner so any explanation would be much appreciated.
All you have to do to fix your program is remove four spaces:
def valueInput():
print("Please enter 20 random numbers")
values = []
for i in range(20):
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
values.append(value1)
return values
The last line in the block above has been unindented by one level. Instead of returning after the first time through the loop, it lets the loop complete, and then returns the resulting list.
Also you can change your:
values = []
for i in range(20):
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
values.append(value1)
return values
part with:
values = [int(input("Enter a random number " + str(x + 1) + ": ")) for x in list(range(20))]
return values
You can check out build in functions

Output is still not iterating

My program still not iterating even count += 1 is implemented in the code, its seems that the code for count += is not being read. Please bear with me how I post my question.
NameInput = int(input("number of students to enter: "))
GradeInput = int(input("number of grades per student: "))
students = {}
class Student():
def GetInfo(students):
idnum = 0
count = NameInput
for c in range(count):
idnum += 1
name =(input("Enter Student " + str(idnum) + " Name: "))
if name in students:
print ("Student " + name + " has already been entered, please enter again.")
idnum -= 1
count += 1
else:
students[name] = []
for r in range(GradeInput):
grade =(float(input("Enter Student " + str(idnum) + " Grade: ")))
students[name].append(grade)
def printList(students):
for key in students:
print(key)
def main():
Student.GetInfo(students)
main()
The problem isn't the count += 1 itself. You can add some print statements throughout your code to see the value of count at various points in the execution.
The problem is that you want to change the number of iterations of your loop from within it. You wrote your loop:
for c in range(count):
The value of count is read when this statement is reached. The value is passed to range(), and the object returned is used to control the loop. Changing the value of count after this has no effect on your program because it is never used again.
One way to change your code would be like this:
c = 0
while c < count:
...
c += 1
This approach allows you to add to count in order to cause the loop to run more iterations. It is more code than the simpler for loop, but is more flexible.

Categories