[Tutor] Tuple/Dict

Martin A. Brown martin at linux-ip.net
Sat Dec 7 11:39:45 EST 2019


Hello Reborn0fDarkness,

You didn't actually show any code, so I'll mention that when you 
show code we can actually help you considerably more.  You asked 
some very simple questions, so I have provided some pointers and 
very simple answers.  You'll still need to assemble the 
remote-controlled car yourself.  See below.

>Hi! i have a test where i have to

>ask the person a number

Try the input() function:

  https://docs.python.org/3/library/functions.html#input

You will need to convert the returned value to some sort of number.  
I'll assume an integer, but you decide what applies to your 
situation.

Something like:

  num = int(input('type a number:  '))

>and then do his divisers

You'll need to supply a function that computes the divisors.

  divisors = your_function(num)

Supposing that your_function() returns a list ....

>make them into an tuple

This is very easy, with the built in function tuple():

  https://docs.python.org/3/library/functions.html#func-tuple

  tdivisors = tuple(divisors)

>add the tuple into a dict

You don't say what the key should be.  But, I'd imagine it'll be the 
original number, so you can set up a loop or whatever to collect 
numbers until your monkey subject tires of entering numbers.

  result = dict
  result[num] = tdivisors

>and then show the dict,

You say "show the dict".  There are so many ways to do this, that 
you have tremendous freedom of how to do that.  If you want to 
control what the output looks like, then you probably will write 
some sort of output presentation function using the very powerful 
new Python string formatting capabilities:

  https://www.python.org/dev/peps/pep-3101/

If you just want something dead simple to produce output, here's (my 
favorite) data structure debugging and diagnostic tool, pprint:

  https://docs.python.org/3.8/library/pprint.html

You can print the data structure with:

  import sys, pprint
  pprint.pprint(result, stream=sys.stdout)  # -- or sys.stderr

>and im losing my mind, any help?

I'm afraid you'll have to show your work on that one.  There is no 
Python module or function to help on that.

Good luck,

-Martin

-- 
Martin A. Brown
http://linux-ip.net/


More information about the Tutor mailing list