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
That's the program code(missing some lines):
It's all about extracting a specific number from a specific string line, and finally counting those numbers together as a float number, and then dividing them by the number of times the numbers were found in every string.
The problem is that the variable nc doesn't count in the following code?!
fname = "files/mbox-short.txt"
try:
fh = open(fname)
except:
print("No such a file, try again..")
quit()
for lines in fh:
if not lines.startswith("X-DSPAM-Confidence:"):
continue
oline = lines.split()
for number in oline:
nc = 0
try:
fnumber = float(number)
nc = nc + 1
print(fnumber, nc)
except:
continue
The file specified in the code
Put nc = 0 out of the loop, otherwise you are zeroing it every time
try:
nc = 0
for number in oline:
try:
fnumber = float(number)
nc = nc + 1
print(fnumber, nc)
except:
continue
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 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am unable to solve this.
Problem Statement:-
Make use of while loop and print all positive even numbers smaller than or equal to the number given by the user
e.g.:-
User Input: 6
Output: 2, 4, 6
I have tried this but the code is running into an infinite loop.
num = int(input("Please enter an even number: "))
i = 0
while num >= i :
if num % 2 == 0:
print(num)
i = num + 1
Thank you.
Don't use a while loop here, just use a for loop:
num = int(input("Please enter an even number: "))
for nr in range(0, num, 2):
print(nr)
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 year.
Improve this question
May I know why there be a invalid syntax on my f variable on the first elif loop?
def bsd():
if price_of_properties <= 180000:
price = 180000*0.1
f = '{0:.2f}'.format(price)
print("BSD is $" + str(f))
elif price_of_properties > 180000 <= 360000:
price = (((180000*0.1) + (price_of_properties - 180000 + (180000* 0.2)))
f = '{0:.2f}'.format(price)
print("BSD is $" + str(f))
elif price_of_properties > 360000 <= 100000:
price = (((180000*0.1) + (180000*0.2) +(price_of_properties - 180000 + (640000* 0.3)))
f = '{0:.2f}'.format(price)
print("BSD is $" + str(f))
Your second and third assignments to the price variable have unbalaned parentheses ((more opens than closes).
This will cause an error on the following lines though I think Python 3.11 may fix that, reporting errors more accurately. You can probably fix this by adding a closing parenthesis on each of those two lines. Or removing the first. But check the formulae after you do so, in case you make the wrong choice.
And, yes, the extra opening parenthesis in that first paragraph was intended :-)
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 11 months ago.
Improve this question
var = input()
if var = ("lookup"):
print(input())
var = 0
It says I need a ":" on the second line but there is already one there. What do I do?
I am not sure what your problem is, but this is some code that runs:
var = input()
if var == "lookup":
print(input()) # printing what is input?
var = 0
You were checking if var is "lookup", but as pointed out in the comments, you were missing an "=" sign. Please edit your post to explain exatcly what is needed (post input / output examples).
To compare var you need to use the double equal symbol (==) instead of the single equal symbol (=). The single equal symbol is used for assignment as you did in the first line (var = input('Enter a string: ')).
var = input('Enter a string: ')
if var == "lookup":
print(input('Enter the second string: ')) # input the value then it prints the same
var = 0
print(v"now var is: {var}")
Output:
Enter a string: lookup
Enter the second string: Demo
Demo
now var is: 0
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
I have to write a function recursively to convert a given decimal string ad return a binary. I can't change the first 3 lines of the function(n = Len(a) ... return str(bin(int(a)))) and I don't understand why my solution doesn't work. Does anyone have any suggestions?
{str} -> {str}
def converttobin(a: str) -> str:
n = len(a)
if n == 1:
return str(bin(int(a)))
else:
return converttobin(str(int(a) % 2)) + str(int(a) % 2)
Instead of sending the remainder, send the quotient str((int)a//2) to the function call.
For decimal to binary conversion, we take remainders as the current output and further apply the conversion on the quotient.
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