Issue
Good evening good people. i am asking for assistance if possible. i am trying to create a simple shopping list that sums the total in the end. however for some reson i cannont understand how i can sum all elements in my list. there are no limits on how many items one can add, so the sum should add all values in range etc.
- how can i make it so my code prints out the summarized value of all numbers under "sum"
my code is as follows
import math
import pandas as pd
myTup = ()
myList = []
def Innlesning():#FUNKSJON FOR INNLESING AV BRUKERNDATA
global myList #DEFINERER LISTE SOM GLOBAL VARIABEL FOR UTHENTING I ANNEN FUNKSJON
global summer
while True:
itemName = input("Navn på produkt ")
if itemName.isalpha():
break
else:
print("Benytt kun bokstaver")
pass
while True:
itemNum = input("Antall ")
if itemNum.isdigit():
break
else:
print("Benytt kun tall")
pass
itemPrice = input("pris på produkt ")
while True:
if itemPrice.isdigit():
break
else:
print("Benytt kun tall")
pass
myTup = (itemName,itemNum,itemPrice,int(itemNum)*int(itemPrice) )
myList.append(myTup)
myList.sort()
lis=list(myList)
def utskrift(): #FUNKSJON FOR UTSKRIFT, MED PANDA TABLE)
global myList
#print(myList)
print(myList)
from tabulate import tabulate
y=len(myList)
headers=["NAVN","ANTALL","PRIS","SUM"]
print(tabulate(myList, headers, tablefmt="grid"))#PRODUSERER TABLE UTSKRIFT
#print("totalsum for dine varer er = " + """Summer kolonne""" + " .-Nkr")
###hvordan kan jeg summere
#totalsum = sum(list(myList[0]))
#print("Totalsum er:" + totalsum)
return myList
pass
while True:
Innlesning()
i = input("Trykk Enter for nytt produkt - ELLER skriv inn 0 FOR UTSKRIFT: ")
if i =="0":
break
print("Your input:", i)
utskrift()
print("AVSLUTTER")
#HENTER UT FUNKSJONENE FOR INNDATA, DERETTER UTSKRIFT"
Solution
...
myTup = (itemName,itemNum,itemPrice,int(itemNum)*int(itemPrice) )
myList.append(myTup)
...
i will assume that the number you wanted to add together is int(itemNum)*int(itemPrice)
, which is every last element of the tuple (myTup[-1]
)
what you can do is:
...
total = 0
for tuple in myList:
total += tuple[-1]
...
when the loop is done, the total
should be the number you want
Answered By - uwuK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.