My value for this always equals 0 [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
tests = int(input("Enter the number of tests needed: "))
goal = int(input("Enter the desired sum: "))
counter1 = 0
counter = 0
total = 0
while counter != tests:
counter = counter + 1
while counter1 != goal:
total = total + counter1 **2
counter1 = counter1 + 1
print(total)
Here is the direct question from my homework, i added more stuff, but now it goes on infinitely: Write a program to calculate the sum of squares like 1^2 + 2^2 etc, it should keep going till a sum is reached, output sum every time and do it for a specified number of tests.
The program is supposed to loop till desired sum is reached and out put each total every time, but no total appears to be printed.

You need :
`counter <= goal:`
in your loop. The while tests whether the expression is true and if its true, then the content in the loop is executed. You want to execute it als long as counter and goal are not the same. The right expression for it is: counter <= goal:

Related

My list python code is producing a sum other than what is intended. Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 days ago.
Improve this question
I am trying to code a program that take user input of orders and converts it to a list that I can then produce a sum based on the number of each word in the list. However, my code keeps producing a a sum that is not correct for the provided list. For example, if I enter Water Nachos Water Cheeseburger the intended sum is 24, but my code is producing 39 as the answer. Why is this and what is a potential fix?
x = input("What are your orders?")
orders = list(x.split())
sum = 0
for i in orders:
if i == "Nachos":
sum+=6
if i == "Pizza":
sum+=6
if i == "Water":
sum+=4
if i == "Cheeseburger":
sum+=10
else:
sum+=5
print(sum)
I expected a sum of 24, but got a sum of 39.
The last else part is only for the if condition i == "Cheeseburger"
So `sum+=5 will gets executed for all the other conditions.
You can just use if else if
x = input("What are your orders?")
orders = list(x.split())
sum = 0
for i in orders:
if i == "Nachos":
sum+=6
elif i == "Pizza":
sum+=6
elif i == "Water":
sum+=4
elif i == "Cheeseburger":
sum+=10
else:
sum+=5
print(sum)

Why the While loop continues and how to calculate the average based on numbers input? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
Hi I'm trying to solve this coding homework:
Write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1.
I define the while loop to make sure it keeps asking, as:
while n != -1
str(input("enter your number:"))
But whenever I try to input -1, it just keeps on asking to enter the number regardless.
Also, I'm not sure what is the best way to define the average excluding -1, none of the lessons prior to this assignment talked about this. I have Googled about it but none of the examples match this particular assignment, even fumbling around did not help.
Thank you for your help :)
Presumably n is meant to be the user input, but you're never assigning a value to n. Did you mean to do this?
n = str(input("enter your number:"))
Also, you're comparing n to -1, but your input isn't a number; it's a string. You can either convert the input to a number via n = int(input(...)), or compare the input to a string: while n != '-1'.
You could ask for a number the if it is not equal to -1 enter the while loop. So the code would be:
n = float(input("What number?"))
if n != -1:
sum += n
nums_len = 1
while n != -1:
sum += 1
nums_len += 1
n = float(input("What number?"))
print("The average is", str(sum/nums_len))
Thanks everyone, this is the final code with the correct values that gives the average of user inputs
n = float(input("What number?"))
if n != -1:
sum = 0
nums_len = 0
while n != -1:
sum += n
nums_len += 1
n = float(input("What number?"))
print("The average is", float(sum/nums_len))

What is the meaning of this code, how does the sum of a consecutive number work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
N = int(input())
count = N
x = range(1, N +1)
for i in x:
N = i + N
print(N - count)
can someone tell me and explain how this code works? I have been spending minutes looking at this and still cant figure out whats going on, the concept is called "sum of consecutive numbers"
I tried this code and for example when I do 100 it shows 5,050 i want to understand how.
the given code is unnecessarily complicated and confusing. her a reworked version with some comments:
N = int(input()) # get input and convert it to int
total = 0 # variable to hold the running total
for i in range(1, N + 1): # create numbers from 1 upto N using a range
total += i # build sum and store it in var total
print(total) # output total

No output when trying to print [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to write a python code in which I input a number, which should be a multiple of 3, and calculate the sum of each digit's power of 3,
ig: input'12' => multiple of 3
should return 1^3 +2^3 = 9
I tried this script and didn't get anything when I run it, what's the problem
number= input('give a number: ')
TT=list(number)
if int(number)==0:
print('wrong number')
elif int(number)%3:
for x in (range(len(TT))-1):
aggregate=sum(int(TT[x])**3)
print(f'the aggregate of {number} is {aggregate}')
None of the conditions are true, so no value ever gets displayed. If you input 12, then 12 % 3 equals 0 (False).
Take a look at the python truth value testing
You should update this line:
elif int(number)%3 == 0:
Now, the whole code needs a revision to get the result that you want. Let me make some changes:
number= input('give a number: ')
aggregate = 0
if int(number)==0:
print('wrong number')
elif int(number) % 3 == 0:
for x in number:
aggregate += int(x) ** 3
print(f'the aggregate of {number} is {aggregate}')

when i put 3 if statements prints all of them [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The following uses variables I have not shown the initialization of, but you can make up your own. It does exactly what I want it to do but it prints all of the if's not just 1 one the if's.
m = input()
if m == 1:
print(l)
time.sleep(1)
else:
print(cost + f + baws)
boss_he = boss_he - 20
print("%" + str(boss_he) + " boss health left")
if m == 2:
time.sleep(1)
else:
print(cost + baws)
boss_he = boss_he - 25
print('%' + str(boss_he) + ' boss health left')
if m == 3:
time.sleep(1)
else:
print(h)
wiz_he = wiz_he + 15
print('%' + str(wiz_he) + " of you health left")
Whenever I run this code it will execute every else case, why?
If each else prints a 1, or a 1, or a 3 if I put that number, it will print 1 2 and 3 instead if just printing 1(if i asked it to print 1). Is it something wrong with my code?
Your input() will never evaluate to any of your if statmenet,s because you are taking input() as a str. Thus, all the elses are triggered. Instead, cast int: m = int(input()). Also, your logic is flawed, because if m is not 1, 2, or 3, all the else statements will be entered.
Why don't you use if,elif,elif and else instead of using if several times? And the program determines your input as string so that make it like m=int(input()) to convert your input to an integer

Categories

Resources