Python code problems i am making a code for class where i need to ask the user 7 questions and get a total sum of carbon
Posted: Fri May 20, 2022 11:02 am
Python code problems
i am making a code for class where i need to ask the user 7
questions and get a total sum of carbon the individual person uses
per year. I was given a dataframe, and told explicitly that i need
to call the dataframe in the code (ie i cant input directly the
numbers then sum them). everything works fine individually and
returns the correct numbers, but when i sum them up, i run into
issues, specifically for questions 6 and 7 of my list of questions.
When one is commented out, the other works perfectly, and vice
versa, but when they both are being run, the program returns one of
the values as 0, instead of the value it should be returning. (see
pictures of output below) How do i fix this
import numpy as np # mathematical import matplotlib.pyplot as plt import pandas as pd
= #6.) ask the user if they have pets print("Do You have any pets ?\n1) No\n2) Yes- 1\n3) Yes-2 vall = int(input("Enter value: ")) info = df.loc(cat.str.contains("Pets", case=False)] idx=df.index [cat=='Pets'] if vall = 1: pet=0. total = pd. Series( [0.3) elif vall == 2: pet-1. total+= m_p.iloc[idx] *freq. iloc(idx] elif vali == 3: pet=2. total += m_p.iloc(idx] *freq. iloc(idx] elif vall == 4: pet=3. total += m_p.iloc[idx] *freq.iloc(idx] else: print('error) total_6= total.sum()*pet print(total_6) #6.) how often do you buy clothes- monthly then add 25% to print("How often do you buy clothes? \n1) monthly \n2) year vali = int(input("Enter value: ")) info = df.loc(cat.str.contains ("Clothing", case=False)] idx=df.index [cat='Clothing 'l if vali =1: total+= m_p.iloc (idx] *freq.iloc(idx] clothes= 12. elif vali ==2: total += m_p.iloc(idx] *freq.iloc(idx) clothes = 1. elif val1==3 : total += m_p.iloc(idx] *freq.iloc idx] clothes = 6. else: print('error') total_7 = total.sum()*clothes print(total_7) total = total_6 + total_7 print (total)
Do You have any pets ? 1) No 2) Yes-1 3) Yes-2 4) Yes-3 Enter value: 2 1080.0 How often do you buy clothes? 1) monthly 2) yearly 3) bi-yearly Enter value: 2 0.0 1080.0
i am making a code for class where i need to ask the user 7
questions and get a total sum of carbon the individual person uses
per year. I was given a dataframe, and told explicitly that i need
to call the dataframe in the code (ie i cant input directly the
numbers then sum them). everything works fine individually and
returns the correct numbers, but when i sum them up, i run into
issues, specifically for questions 6 and 7 of my list of questions.
When one is commented out, the other works perfectly, and vice
versa, but when they both are being run, the program returns one of
the values as 0, instead of the value it should be returning. (see
pictures of output below) How do i fix this
import numpy as np # mathematical import matplotlib.pyplot as plt import pandas as pd
= #6.) ask the user if they have pets print("Do You have any pets ?\n1) No\n2) Yes- 1\n3) Yes-2 vall = int(input("Enter value: ")) info = df.loc(cat.str.contains("Pets", case=False)] idx=df.index [cat=='Pets'] if vall = 1: pet=0. total = pd. Series( [0.3) elif vall == 2: pet-1. total+= m_p.iloc[idx] *freq. iloc(idx] elif vali == 3: pet=2. total += m_p.iloc(idx] *freq. iloc(idx] elif vall == 4: pet=3. total += m_p.iloc[idx] *freq.iloc(idx] else: print('error) total_6= total.sum()*pet print(total_6) #6.) how often do you buy clothes- monthly then add 25% to print("How often do you buy clothes? \n1) monthly \n2) year vali = int(input("Enter value: ")) info = df.loc(cat.str.contains ("Clothing", case=False)] idx=df.index [cat='Clothing 'l if vali =1: total+= m_p.iloc (idx] *freq.iloc(idx] clothes= 12. elif vali ==2: total += m_p.iloc(idx] *freq.iloc(idx) clothes = 1. elif val1==3 : total += m_p.iloc(idx] *freq.iloc idx] clothes = 6. else: print('error') total_7 = total.sum()*clothes print(total_7) total = total_6 + total_7 print (total)
Do You have any pets ? 1) No 2) Yes-1 3) Yes-2 4) Yes-3 Enter value: 2 1080.0 How often do you buy clothes? 1) monthly 2) yearly 3) bi-yearly Enter value: 2 0.0 1080.0