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 2 years ago.
Improve this question
Given 3 int values, a b c, return their sum. However, if any of the values is a teen -- in the range 13..19 inclusive -- then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper "def fix_teen(n):"that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). Define the helper below and at the same indent level as the main no_teen_sum().
here's my code:
#Given 3 int values, a b c, return their sum. However, if any of the values is a teen -- in the range #13..19 inclusive -- then that value counts as 0, except 15 and 16 do not count as a teens. Write a #separate helper "def fix_teen(n):"that takes in an int value and returns that value fixed for the teen #rule. In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). Define the helper #below and at the same indent level as the main no_teen_sum().
def no_teen_sum(a, b, c):
a = fix_teen(a)
b = fix_teen(b)
c = fix_teen(c)
return(a+b+c)
def fix_teen(n):
if n<=13 and n>=19 and n != 15 and n != 16:
n = 0
return(n)
Don't know why this doesn't work. I'm pretty sure my code is correct but my algorithm is wrong. Also this is my first question please correct me if my format is incorrect.
You might mean if n>=13 and n<=19 and n != 15 and n != 16, because with the greater than and less than signs swapped no integer will ever fulfill the condition.
As Chris pointed out, you have the operators the wrong way around. You need if n>=13 and n<=19, but have if n<=13 and n>=19.
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 3 years ago.
Improve this question
I am new to programming and have just briefly learned functions in python. I do not understand why print(len(str)) don't work the same as return len(str) in a function.
I have tried both print and return for the last statement of the function and am confused about my understanding of len(). I need some guidance, thank you! Perhaps someone can guide me as to how I can further improve my foundation as I am still pretty new to programming. Thank you!!
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
numDigits(833)
If I change the
print to return len(str_digits) and
numDigits(833) to print(numDigits(833)),
I get my expected answer.
What i expected:
3
Actual result:
3
None
1
2
4
3
In the first case numDigits doesn't return a value from the function, and you only print it inside the function
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
print(numDigits(833))
The output here is
3
None
The 3 comes from print and None comes from the function, and when you print it, it prints None
If you want to return, you need a return statement like return len(str_digits) at the end of the function like so
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
#Return statement
return len(str_digits)
print(numDigits(833))
The output will now be
3
3
Now the first 3 comes from print, and the second 3 comes when you print what numDigits return, which is 3
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have 2 questions I need to ask for help with. One question I don't entirely understand so if someone could help me to that would be great.
Question One (the one I don't entirely understand):
One definition of e is
Formula for e
This can be calculated as my_e = 1/math.factorial(0) + 1/math.factorial(1) + 1/math.factorial(2) + 1/math.factorial(3) + …
Let n be the input number of the math.factorial() function. n successively takes on 0, 1, 2, 3 and so on. Find the smallest n such that the absolute value of (my_e – math.e) is less than or equal to 10-10. That is, abs(my_e - math.e) <= (10 ** -10).
I just don't entirely understand what I am being asked to do. Clarification would be great. Thanks!
Question 2:
Ask the user to type in a series of integers. Sum them up and print out the sum and the number of integers the user has entered.
My code
So what should happen is after I enter the numbers I want to enter and hit the enter key, it should calculate and print out "sum = 25 count = 3". The screenshot shows what error message I am getting.
Any help you have is welcomed and greatly appreciated.
As far as you first question goes:
>>> import math
>>> math.e
2.718281828459045
>>> sum(1.0/math.factorial(i) for i in range(5))
2.708333333333333
>>> abs(sum(1.0/math.factorial(i) for i in range(5)) - math.e) < 10**-10
False
>>> abs(sum(1.0/math.factorial(i) for i in range(30)) - math.e) < 10**-10
True
So, somewhere between n == 5 and n == 30 you get 10 decimal places for e. Create the sum term by term in a while-loop (instead of by using the sum function as I have, since you are unlikely to have seen that syntax yet). At each pass through the loop, compare the sum with math.e. Stop when you get the target accuracy. Return the final n.
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
It says that I have a syntax error. But I don't see whats wrong. It's on the line where it says a.append. I'm so confused. I apologize i'm a newbie. I need some help. I'm pretty sure I have more than one. But the first one is on a.append(0)
def invalidsyntax(a):
if max(a)>20:
print("The highest number is in position", a.index(max(a)))
a.remove(max(a))
if min(a)>5:
print("The smallest number is", (min(a))), index.append(min(a)
a.append (0)
if min(a)>5
a.sort(['a'])
sum('a') / float(len('a')
a[2]
a[-3]
TL;DR: you are missing at least one parenthesis, and how you place the parentheses correctly depends on whether you are using Python 2 or Python 3.
Count your parentheses:
print("The smallest number is", (min(a))), index.append(min(a)
^ ^ ^ ^^^
| | |_|||
| |______||
|__________________________________|
In either Python 2 or Python 3, the syntax error is the missing parenthesis for index.append(min(a)).
However, just supplying that parentheses doesn't entirely fix your problem. Python 2 and Python 3 will interpret the resulting line slightly differently. Suppose you write the following:
print("The smallest number is", (min(a))), index.append(min(a))
In Python 2, you have a print statement provided with two expressions: the tuple ("The smallest number is", min(a)) (the parentheses around min(a) are redundant) and the call to index.append. The value of each expression is printed on the same line, separate by a space. Since index.append always returns None, the output is
# Assuming min(a) returns 9
("The smallest number is", 9) None
In Python 3, you have an expression statement, consisting of a call to print("The smallest number is", min(a)) (again, the parentheses around min(a) are redundant) and a call to index.append. print is a function in Python 3. Now the output is just
The smallest number is 9
and the value of the expression is the tuple (None, None) (the return values of the two functions.)
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
So I am trying to write a program that takes positive,negative numbers and then displays them at the end once 0 is entered,
pos1=0
neg1=0
all1=0
flt1=float(raw_input("enter a non-zero floating point number (decimals): "))
while(flt1!=0.0):
if (flt1 < 0.0):
neg1=neg1+flt1
all1=all1+flt1
elif(flt1 > 0.0):
pos1=pos1+flt1
all1+all1+flt1
print "the sum of all numbers entered is ",all1,"the sum of all positive numbers are ",pos1,
print "and the sum of all negitive numbers are ",neg1,
my issue is that when I actually trace it, ( if I take say 5.0 and trace it) I see that it gets stuck on the "elif" part of the code and doesn't actually come back to ask for another number. I am stuck on trying to figure out what I need to do for it to come back out and ask for another non-zero number. My goal is to have the user keep entering numbers til he/she enters 0 then it would take all the negatives and add them together and display them, then do the same for the positives and then display the entire sums (negs and pos) so far entering 0 works but nothing else
Edit: fixed indentation on the elif loop
just put the flt1=... line inside the loop.
also, you should generally avoid float equality comparisons, although zero is ok.
and you should use whitespace (n = 72 + 61 * x, not n=72+61*x).
pseudocode:
while True:
num = input
if num > 0.0:
do_stuff()
elif num < 0.0:
do_neg_stuff()
else:
break
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is my problem ? I want act inverse number--example : 123 ==>321
def loop(a):
i=0
while(a>=1)
print(a%10)
s=s/10
i=i+1
Your solution has a few problems, aside from the indentation and the missing colon.
First of all your are using print which automatically adds a line break, so that might not be what you want the result to look like. You could store the result in a string which you append the latest character to and then print it once at the end.
Further, you are using a variable s which was never used before. In thise case it should be a as you want to strip off the last digit using an integer division by 10. Note that in this case, this will only work like that in Python 2, as Python 3 will use a float division there (e.g. 15 / 10 == 1.5). You can prevent that by explicitly using the integer division there (this will also make your intend more clear): s = s // 10 (note the two slashes).
Lastly, you are incrementing the variable i without ever using it, so you can just get rid of it.
In the end, it might look like this:
def reverse (a):
rev = ''
while a > 1:
rev += str(a % 10)
a = a // 10
A shorter solution, utilizing the fact that you can just reverse strings:
>>> num = 123
>>> rev = int(str(num)[::-1])
>>> rev
321
If you leave out the int(), you can even keep trailing/leading zeros and get a string instead:
>>> num = 3210
>>> str(num)[::-1]
'0123'
Few issues:
Your indentation does not match. PEP 8 suggests 4 spaces for indentation.
You're missing a colon after while(a>=1)
Although this isn't an issue, you don't need the parentheses in the while loop, it can just be while a >= 1
s = s/10 might not return what you expect. For example, 12/10 == 1 (unless you're dealing with floats here).
This can all be simplified using slicing:
>>> print int(str(123)[::-1])
321
It is important to indent correctly. (And don't mix tabs and spaces.)
def loop(a):
i = 0
while a >= 1:
print(a % 10)
a = a / 10
i = i + 1
You were also missing a colon after the while condition.