Python Forum
Writing a code with 3 digit numbers while they cant contain 0 and then sum them
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing a code with 3 digit numbers while they cant contain 0 and then sum them
#1
Hey i need to write a code for my teacher, he said Write a code that will have three 3digit numbers in it and if the number contains 0 f.e. 300,301 or so it will be removed. Afterwards i need to sum the numbers that are left.
So far i created a list with 3 variables in it a,b,c and before i defined a,b,c as random.randint(111,999) so it will be a 3 digit number.
My problem is that i dont know how to find a specific thing in a number. I tried changing the a to string and then doing if 0 in a: /nameoflist/.remove(a) but idk
import random
a=[random.randint(111,1000)]
b=[random.randint(111,1000)]
c=[random.randint(111,1000)]
cisla=[a,b,c]
str(a)
if 0 in a:
    cisla.remove(a)
print(cisla)
Note: cisla is name of list and in english it means numbers so u wont get confused :D
Reply
#2
I tried your code with a = str(a) instead of just str(a) and it worked.
Reply
#3
In order to randomly pick from all three-digit numbers shouldn't you start with 100 instead of 111?

You should also be aware that random.randint(a, b) returns a random integer N such that a <= N <= b This means that random.randint(111, 1000) can return 1000 (which is not three-digit)

You could take advantage of random.sample():

>>> cisla = random.sample(range(100,1000), 3)
Part of your problem is:

>>> a=[random.randint(111,1000)]    # you create list with random int in it
>>> a
[433]
>>> str(a)                          # you created string from list containing one int; didn't assign name
'[433]'
>>> a                               # a is still list with random three-digit int in it
[433]
>>> if 0 in a:                      # there can't be 0 in a as element because there can be three-digit integers only
In you code cisla is list of lists, something like: [[457], [817], [844]]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 281 Jan-27-2024, 05:23 PM
Last Post: Winfried
  writing and running code in vscode without saving it akbarza 1 344 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  prefix ID Number with 0,00 make 3 digit. mg24 1 708 Oct-06-2022, 07:20 AM
Last Post: ibreeden
  if a string has a digit - print tester_V 2 2,081 Jan-16-2021, 04:48 AM
Last Post: tester_V
  Help with writing or plan part of code Merlin_1 1 1,778 Aug-24-2020, 03:28 AM
Last Post: Larz60+
  Is this right code for 3 digit pin ? fatjuicypython 1 1,785 Aug-21-2020, 11:15 PM
Last Post: bowlofred
  Writing a basic shift code djwilson0495 2 2,218 Aug-16-2020, 01:52 PM
Last Post: djwilson0495
  a better way of writing code Than999 6 3,281 Jun-13-2020, 08:02 AM
Last Post: pyzyx3qwerty
  can you help me out with the code for reversible numbers under 100? Bhavika 5 2,557 Apr-05-2020, 03:36 PM
Last Post: ndc85430
  code not writing to projNameVal portion of code. umkc1 1 1,639 Feb-05-2020, 10:05 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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