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.)
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 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.
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
square = 0
number = 1
while number < 10:
square = number ** 2
print(square)
number += 1
It's my answer for this question :" Print all squares from 0 to 99(1,4,..,81)Use number variable in while loop."
Pycharm says it doesn't match with its answer.
I think i should print those numbers in a single line but i couldn't deal with it.How can i do that ?
Your code will print each number in a newline, because, in Python, the call to print() comes with an implicit newline (see the end argument per the documentation).
Next, you're making an assumption about output formatting. As I see it, there are two primary issues with this assumption:
1) Calls to functions (e.g. print()) in a while loop "execute" when they're called -- there's no delay to see if a future pass through the loop will provide extra data to the function.
2) You're assuming that the Python interpreter will guess that printed numbers (in a while loop) are desired to be returned in a comma separated list. Computers are machines that do what you tell them to do -- if you don't write logic to explain what you need, the machine cannot give you this.
You can express your desired output in the following ways:
1) Collect the numbers (as strings) in a list, then output them after you're done looping:
square = 0
number = 1
# think of this as a result container
number_result_list = []
while number < 10:
square = number ** 2
# place that number at the end of your container:
number_result_list.append(str(square))
number += 1
# join all your number strings together, using a comma
print(",".join(number_result_list))
# prints 1,4,9,16,25,36,49,64,81
2) Specify that you want to use a comma in the call to print. Make special note of the trailing comma -- you now know why this happens:
square = 0
number = 1
while number < 10:
square = number ** 2
print(square, end=",")
number += 1
# prints 1,4,9,16,25,36,49,64,81,
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 variable x always shows up as a syntax error. It means number of prime numbers to be generated
x=501
while x<1 or x>500:
NoNos=int(input("Number of Prime Numbers"))
if x<1:
print("The number has to be greater than 1")
if x>500:
print("The number has to be lesser than 500")
PrimeNo=2
PrimeNos=[]
While not x==0:
if PrimeNo==2:
PrimeNos=PrimeNos+[PrimeNo]
x=x-1
PrimeNo=PrimeNo+1
continue
for divisor in the range (2,PrimeNo-1):
if not PrimeNo%divisor=0:
x=x-1
PrimeNos=PrimeNos+[PrimeNo]
print(PrimeNos)
$ python test.py
File "test.py", line 11
While not x==0:
^
SyntaxError: invalid syntax
This is a bug in Python. The caret should be pointing to the capital 'W' in 'While'. You must spell 'while' with all lowercase letters.
You have some other typos as well:
File "test.py", line 17
for divisor in the range (2,PrimeNo-1):
^
SyntaxError: invalid syntax
Here the caret should point at the 'the', which should just be removed.
File "test.py", line 18
if not PrimeNo%divisor=0:
^
SyntaxError: invalid syntax
This time the caret is in the right place! '=' needs to be '=='.
After I make all those changes your program still does not work, but the remaining problems do not seem to be syntax.
(I filed http://bugs.python.org/issue23518 on the misplaced carets.)
I fixed some errors in your code
While => while
the range => range
PrimeNo%divisor=0: => PrimeNo%divisor==0:
=======================================================================
x=501
while x<1 or x>500:
NoNos=int(input("Number of Prime Numbers"))
if x<1:
print("The number has to be greater than 1")
if x>500:
print("The number has to be lesser than 500")
PrimeNo=2
PrimeNos=[]
while not x==0:
if PrimeNo==2:
PrimeNos=PrimeNos+[PrimeNo]
x=x-1
PrimeNo=PrimeNo+1
continue
for divisor in range (2,PrimeNo-1):
if not PrimeNo%divisor == 0:
x=x-1
PrimeNos=PrimeNos+[PrimeNo]
print(PrimeNos)
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