Python Forum
Calculator code issue using list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator code issue using list
#1
The problem is that when I choose / and input 5 and 0 (2nd number as 0) it is giving 0.0 but not printing the divisior is zero


#!/usr/bin/python3
num=input("please select operator +,-,*,/ or q to quit")
while num != 'q':
   numlist=input("please enter at least 2 numbers ").split()
   numlist=[float(i) for i in numlist]
   total = numlist[0]
   del numlist[0]
   if num == '+':
      for i in numlist:
         total = i + total
      print (total)
   elif num == '-':
      for i in numlist:
         total = i - total
      print (total)
   elif num == '*':
      for i in numlist:
         total = i * total
      print (total)
   elif num =='/':
      if i in numlist ==0:
         print ("divisior is zero")
      else:
         for i in numlist:
            total = i / total
         print (total)
   num=input("please select operator +,-,*,/ or q to quit")
Reply
#2
I think you need line 21 to be:
if 0 in numlist:
Reply
#3
Thank you that helped but can you explain why cant we substiute i because i can be any number in list
(Jun-11-2021, 10:01 PM)topfox Wrote: I think you need line 21 to be:
if 0 in numlist:
Reply
#4
The line
if i in numlist ==0:
will give NameError: name 'i' is not defined
if i was defined it would return True if i was in numlist and then do a comparison of True == 0 which would be False
If you want to check if something is in a list just do
numlist = [5, 0]
print(0 in numlist)
Output:
True

(Jun-11-2021, 10:04 PM)kirt6405 Wrote: Thank you that helped but can you explain why cant we substiute i because i can be any number in list
You are not for looping to give i a value like in the other elif statements.
Reply
#5
I believe that i is not defined at line 21, and I'm not quite sure what you are trying to do.

i could be used instead of 0, if a value had been assigned to it, like this:

i = 0
if i in numlist
the expression '0 in numlist' is evaluated as True if the value 0 matches any item in the list numlist.
the expression 'i in numlist == 0' would I suppose be evaluated as True if 'i in numlist' evaluated as False. Then 'False == 0' would be True. That means the expression 'i in numlist == 0' is equivalent to 'i not in numlist', hence you may get the opposite answer to what you expect.
I hope that makes sense!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 503 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  needing some help to write some code for a game calculator rymdaksel 1 400 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  Updating Code And Having Issue With Keys Xileron 8 1,132 May-25-2023, 11:14 PM
Last Post: DigiGod
  Python List Issue Aggie64 5 1,604 Jun-30-2022, 09:15 PM
Last Post: Aggie64
  List to table issue robdineen 2 1,457 Nov-07-2021, 09:31 PM
Last Post: robdineen
  NameError issue with daughter's newb code MrGonk 2 1,448 Sep-16-2021, 01:29 PM
Last Post: BashBedlam
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,199 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  For List Loop Issue Galdain 2 2,045 Dec-31-2019, 04:53 AM
Last Post: Galdain
  IndexError: List index out of range issue Adem 1 3,519 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  Issue with code for auto checkout nqk28703 2 2,156 Nov-01-2019, 09:33 AM
Last Post: nqk28703

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020