While Loop displaying even numbers in Python [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 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)

Related

I am getting "TypeError: '>' not supported between instances of 'float' and 'str'" when I finish this code. What am I doing wrong? [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 2 years ago.
Improve this question
Here is the code
wagesAmount = float(input("Enter Wages Amount: "))
numberOfHours = float(input("Enter Number Of Hours Worked: "))
totalWagesAmount = (wagesAmount) * (numberOfHours)
if totalWagesAmount > "0":
printtotalWagesAmount
I need to add an else statement as well but I was just trying to troubleshoot this issue first
"0" means it's a string . so when u wanna do if something > 0: thats means that 0 is integer and you say (if somthing > "0" string. so you
wagesAmount = float(input("Enter Wages Amount: "))
numberOfHours = float(input("Enter Number Of Hours Worked: "))
totalWagesAmount = (wagesAmount) * (numberOfHours)
if totalWagesAmount > 0:
print (totalWagesAmount)
You have to remove the "" on 0 because "0" != 0
if totalWagesAmount > 0:
Writing "0" means its string because of dynamic typing in python.

Problem with the for loop iteration variable [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
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

Python BMI Calculator I cannot figure out the issue. [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
So I get this error when I run the program, I am not sure why this is not working.
C:\Python27>python bmi.py
File "bmi.py", line 11
def getweight(wt)
^
SyntaxError: invalid syntax
print('BMI Calculator!')
#declaring my variables
ht=height
wt=weight
bm=bmi
#Calling my main function
main()
#getting user input for weight in pounds
def getweight(wt)
weight = int(input('Please enter your weight input pounds(whole number): '))
return(wt)
#getting user input for height in inches
def getheight(ht)
height = int(input('Please enter your height input in inches(whole number): '))
return(ht)
#function for the bmi calculator
def bmicalc()
bmi = (wt*703)/(ht*ht)
#main function
def main()
getweight()
getheight()
bmicalc()
You need a colon at the end of your function declaration. Like so:
def foo(bar):
pass

traced my code and it seems to be in a continous loop [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 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

what is invalid about this syntax? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
I'm writing code to simulate x number of craps games and I have some code written that may or may not work. I'm not sure yet but I can't check because when I finish editing the module a syntax error notifies me that my code doesn't make sense. The thing is though I feel like I've used this syntax hundreds of times before. The error message reads
File "<ipython-input-5-9e8dfc269e5b>", line 7
if roll=7 or roll=11:
^
SyntaxError: invalid syntax
It doesn't like the equals sign? I don't understand why python can't read this. What is wrong?
here is all of the code for the simulation if it helps
def crap(x):
from random import randint
win=0
loss=0
for i in range(x):
roll=rand(2,12)
if roll = 7 or roll = 11:
win=win+1
elif roll = 2 or roll = 3 or roll = 12:
loss=loss+1
elif roll=4 or roll=5 or roll=6 or roll=8 or roll=9 or roll=10:
rolln=randint(2,12)
while rolln =! 7 or rolln =! roll:
rolln=randint(2,12)
if rolln = 7:
loss=loss+1
elif rolln = roll:
win=win+1
= is an assignement operator, you cannot use that in if-elif conditions. Use == for equality check.
if roll == 7 or roll == 11:
Secondly you can also reduce expressions like:
elif roll==4 or roll==5 or roll==6 or roll==8 or roll==9 or roll==10:
to:
elif roll in (4, 5, 6, 8, 9 ,10):
= is used to assign values to variables. Since you're trying to test for equality, you should be using ==, e.g.
if roll == 7

Categories

Resources