How to create a while loop 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 2 years ago.
Improve this question
I need to create a code that returns the following situation. A date range with only month and year using while and exporting it to a text file using Pandas
I created the first while but I need to add a second one that adds a year each time the month reaches 12:
mnst=6
yrst=2010
mned=12
yred=2020
while mnst<=mned:
print(mnst,"/",yrst)
mnst + 1

You could use a generator function:
def iterate_ym(yr, mn, yred, mned):
while (yr, mn) <= (yred, mned):
yield (yr, mn)
mn += 1
if mn > 12:
yr += 1
mn = 1
for yr, mn in iterate_ym(2010, 6, 2020, 12):
print(yr, mn)

Related

I am receiving a syntax error every time I write an if statement on python, any ideas? [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 5 months ago.
Improve this question
I am a total beginner working on an introductory python class but I am getting syntax errors every time I make an if statement. Can anyone help with this?
Here is my code:
def report_size(n: int) -> str:
"""Return 'small' if n is between 0 and 20 inclusive,
'medium' if n is between 21 and 40 inclusive,
and 'large' if n is 41 or greater.
Precondition: n >= 0
>>> report_size(4)
'small'
>>> report_size(24)
'medium'
>>> report_size(45)
'large'
"""
If n <= 20:
return 'small"
elif 20< n <= 40:
return 'medium"
else:
return 'large'
if should not be capitalized.
Also, the apostrophes you use for strings are inconsistent. If you open a string with ' you have to close with ' and if you open with " you should close the string with ".

Why is my code not working whenever I try do an F-string [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 1 year ago.
Improve this question
age = 17
age_last_year = age - 1
print(f"last year it was {age_last_year} 16.")
I know its extremely basic and it's my first-day coding but I'm so lost
thanks
xd
You forgot " at the end of print statement.
age = 17
age_last_year = age - 1
print(f"last year it was {age_last_year}.")
Output
>>> last year it was 16.
Or you can do using format
age = 17
age_last_year = age - 1
print("last year it was {}.".format(age_last_year))
Output
>>> last year it was 16.

Python GCD program [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
def gcd(m,n):
fm = []
for i in range(1, m+1):
if (m%i) == 0 :
fm.append(i)
fn = []
for j in range(1, n+1):
if (n%j) == 0 :
fm.append(j)
cf = []
for f in fm:
if f in fn:
cf.append(f)
return(cf[-1])
print(gcd(56,23))
Although, there are other ways to find gcd using while loop and math.gcd commands, but my mentor wants this solution working as its mentioned in his programming book. How can I solve IndexError: list index out of range in the above program.
You don't append anything to cf.
In your 2nd for loop, change fm.append(j) to fn.append(j)

Temperature conversion Table [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
I need to write a program that displays Celsius to Fahrenheit temperature conversion in a table.
Question - What is the correct way to print a specific index within a list. My attempt comes from the answer to a similar exercise outlined here. Thanks in advance.
This is my code
temp = range(0 , 101, 10)
tbl = []
tbl2 = []
for i in temp:
cel = i
tbl.append(cel)
fah = (i * 9/5) + 32
tbl2.append(fah)
print(cel, fah)
print('Celsius\t\tFahrenheit')
print('-------------------')
print(tbl(0) + ':\t\t', tbl2(0))
print('-------------------')
This is my output
Functions are called with parenthesis(). Indexes are accessed with with brackets[0]:
print(tbl[0] + ':\t\t', tbl2[0])

Why this the result of this summation code 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
I was following a tutorial online and I did the same as the video but it gives me a different result?
total1 = 0
for b in range(1,5):
total1 += b
print(b)
The total should be 10, but instead I get 4. What did I do wrong?
You should be printing total1, instead of b.
This would be your code:
total1 = 0
for b in range(1, 5):
total1 += b
print(total1)
Hope this helps!

Categories