Parth1234
Posts: 84
Joined: Mon Mar 12, 2018 3:14 pm

print the output using python

Thu Apr 05, 2018 7:07 pm

I am looking help with following python code.

Code: Select all

 
# Main loop.
while True:
    # Read all the ADC channel values in a list.
    values = [0]*4
    for i in range(4):
        # Read the specified ADC channel using the previously set gain value.
        values[i] = adc.read_adc(i, gain=GAIN)
    # Print the ADC values.
    print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*values))
    # Pause for half a second.
time.sleep(0.5
I want to replace below line
# Print the ADC values.
print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*values))
I want to print each output like value(0), value (1), value(2), value(3). like table of four variable. How to do that

User avatar
paddyg
Posts: 2393
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: print the output using python

Thu Apr 05, 2018 10:28 pm

The existing code looks to nearly do what you describe. Unless you mean you want to do something different?
see https://docs.python.org/3.5/library/str ... i-language for how format works

Code: Select all

print('{}, {}, {}, {}'.format(values[0], values[1], values[2], values[3])) # *values is a curt way of doing this
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: print the output using python

Fri Apr 06, 2018 6:03 am

What's the problem with your code? This line:

Code: Select all

print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*values))
should work fine as paddyg says. You don't even have to use all the values in the same line. e.g. you can just do:

Code: Select all

print('| {3:>6} |'.format(*values))
and the line is still valid.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”