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}')
Related
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)
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 1 year ago.
Improve this question
I'm currently studying Python and I've been stuck at this problem for a few hours now. I was given a task to take user input of numbers and split those numbers, and display numbers greater than 3, example:
Input: 12345
Output: 4
5
I had some progress with splitting those numbers, and now I'm at this part where it's supposed to figure out if the number is greater than 3 but I keep on getting "TypeError: '>' not supported between instances of 'list' and 'int' "
Anyway, here's my code:
num_input = list(input('Enter number: '))
for i in num_input:
if num_input > 3:
print(i)
Ok, it should be:
num_input = input('Enter number: ')
for i in num_input:
if int(i) > 3:
print(i)
Basically, you just get a single char from the string 12345 at a time and convert it to an int. Then you can compare it to 3 normally.
If you need a shorter version:
num_input = input('Enter number: ')
print([int(i) for i in num_input if int(i) > 3])
Which you output: [4, 5] if you input 123456
You have to make the objects inside the list as integers and then only you can compare them to an integer, in your case greater than three. This should work
num_input = list(map(int,input().split()))
for i in num_input:
if i > 3:
print(i)
This allows to input multiple values in a single line
You don't need the list(). Your input is string, you can iterate through every character of string, cast it to int and then compare:
num_input = input('Enter number: ')
for i in num_input:
if int(i) > 3:
print(i)
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 6 years ago.
Improve this question
number = [1,3,5]
position = 0
while
position < len(number):
numbers = number[position]
if numbers % 2==0:
print('found even number',numbers)
break
position = position +1
else:
I got SyntaxError: invalid syntax, after i push enter after else:
Help me please
The indentation of your position = position + 1 statement is wrong.
It is at the same level as your if and else statements, so separates them.
Indent or move it and you'll be fine.
Also, you could simplify the code a bit by changing the while loop to a for loop as:
number = [1, 3, 5]
for num in number:
if num % 2 == 0:
print("Found even number", num)
else:
Hard to say exactly given the short snippet of code, but it looks like the while loop is unnecessary given what is there
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
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: