print values from list inside a list - list

i have this code in python:
sensor16=['1','-','\\','/','*','!']
sensor15=['4','G','H','I']
sensor14=['7','P','Q','R','S']
sensor13=['*']
sensor12=['2','A','B','C']
sensor11=['5','J','K','L']
sensor10=['8','T','U','V']
sensor09=['0',' ']
sensor08=['3','D','E','F']
sensor07=['6','M','N','O']
sensor06=['9','W','X','Y','Z']
sensor05=['#']
sensor04=['BACKSPACE']
sensor03=['DELETE ALL']
sensor02=['READ']
sensor01=['TRANSMITE']
sensor= [sensor01,sensor02,sensor03,sensor04,sensor05,sensor06,sensor07,sensor08,sensor09,sensor10,sensor11,sensor12,sensor13,sensor14,sensor15,sensor16]
press=[1,1,1,1,1,5,4,4,2,4,4,4,1,5,4]
num_press=0
for steps in range(15) :
sensor[steps]
num_press=press[steps]
for i in range(num_press) :
print(sensor[steps][num_press])
How can I access the value in each sensorXX list which corresponds to the value in press[] list?
For example press[9] is 4, so I want to print sensor10[4] which is V
the reason that i have to go through press[] list is that i have already managed to get some timing functions so i know how much time passed since my last press, so i can either printout the next character inside the specific sensor number list ( e.g sensor01[] or sensor[12] ) and when i reach the maximum number of presses to reloop or i have to move my cursor one place right and start from begin.
i have already build and running in arduino but the code is in C. now i would like to move everything to my raspberry pi 2 and in python.
this was where the first idea came from, and i actually used most of that code to do so in arduino.
youtube video for arduino use of my code
arduino code

I ran your code and got index out of range error. It looks like you just have off by one error. Try this: (Also removed your nested for loop it seems like a mistake)
for steps in range(15) :
sensor[steps]
num_press=press[steps]
print (sensor[steps]) [num_press-1]
Output:
TRANSMITE
READ
DELETE ALL
BACKSPACE
#
Z
O
F
V
L
C
*
S
I

DOOOOONE ! AND THANK YOU ALL FOR YOUR RESPONSE ! :D
sensor16=['1','-','\\','/','*','!']
sensor15=['4','G','H','I']
sensor14=['7','P','Q','R','S']
sensor13=['*']
sensor12=['2','A','B','C']
sensor11=['5','J','K','L']
sensor10=['8','T','U','V']
sensor09=['0',' ']
sensor08=['3','D','E','F']
sensor07=['6','M','N','O']
sensor06=['9','W','X','Y','Z']
sensor05=['#']
sensor04=['BACKSPACE']
sensor03=['DELETE ALL']
sensor02=['READ']
sensor01=['TRANSMITE']
sensor=[sensor01,sensor02,sensor03,sensor04,sensor05,sensor06,sensor07,sensor08,sensor09,sensor10,sensor11,sensor12,sensor13,sensor14,sensor15,sensor16]
press=[1,1,1,1,1,5,4,4,2,4,4,4,1,5,4]
num_press=0
steps=0
for steps in range (15):
for i in range (press[steps]):
print (sensor[steps][i])
and it goes to every value individually.
again ! thanks a lot ....

Related

How to choose randomly from a list, inside a dictionary

I have been trying to make a "Workout Generator" that uses my input to give me a gym workout that focuses on specific muscles or body parts depending on my choice ("input").
I am stuck because I am now trying to get a random workout from a list (Which is a dictionary value) while being in a randomly chosen key inside a dictionary.
This is my first solo project and nothing i have tried has been working so far and I really want to be able to complete this :)
Here is the code so far, I have put the line of code that accepts input as a note so that when i run to test i dont have to keep inputing the same thing, and also made a note the second line of code in my if statement because if I fix this problem with the first one then I can repeat the same for the second one.
Here is a sample of the code
import random
choose_workout = input("Enter body area here: ")
upper_body_cb = {
"chest" : ["Bench press" , "Pushups"],
"back" : ["Lateral Pulldown" , "Pull Up"]
}
random_upper_cb = print(random.choice(list(upper_body_cb.values())))
if choose_workout == "upper c":
print(random_upper_cb)

Python 2.7: How to make a function print an undetermined amount of strings from a tuple

I'm making a text-based adventure game, and would like to have a universal 'look' function that uses an algorithm to tell the player how many and what objects are in a room instead of me having to write individual descriptions for each room. So it would work roughly like this:
lookround(things.bedroom)
You see:
a bed, which is the last on the right, across from Jacob's and to the left of Steve's,
and
a calendar, which is on a nail driven into the wall to the left of your bed
The objects are stored in the class 'things', which has a format that organises them with the object name first, and then the description of its location, then it repeats. That way, all the function has to do is print the first two tuples, then the next two, then the next two, and so on.
So, how would I get it to print out a number of tuples which have not been spoon fed to it?
Right now, I'm trying to use this:
def lookround(room):
print '''You see:'''
for len(room) % 2:
print ('{}, which is {},'.format(room))
The problems I'm having are that I'm getting a syntax error which points to the colon after len, and I'm not sure what I should put in .format() .
I've tried messing around with the syntax, but nothing's working.
class room(object):
things = ('a bed', 'to sleep in',
'a calendar', 'to look up the date',
'a clock', 'to wake up')
def lookround(room):
print '''You see:'''
for i in range(len(room.things)):
if not (i%2):
print ('{}, which is {},'.format(room.things[i], room.things[i+1]))
if i != len(room.things) - 2:
print 'and'
This should work with your current format. It might be a better idea to store things as a tuple of tuples, so you don't deal with the modulus business...

Python how to batch printing x lines at a time in a for loop

I tried all sorts of for loops but just can't seem to figure out how to print "n" number of lines from a dictionary at a time. I am new to programming so please pardon my terminology and expressions...
Example source:
{'majorkey1': [{'name':'j','age':'3','height':'6feet'},
{'name':'r','age':'4','height':'5feet'},
{'name':'o','age':'5','height':'3feet'}],
'majorkey2':[{'name':'n','age':'6','height':'4feet'},
{'name':'s','age':'7','height':'7feet'},
{'name':'q','age':'7','height':'8feet'}]}
This prints everything at once (undesired):
for majorkey in readerObj.keys():
for idx, line in enumerate(readerObj.get(majorkey)):
print line
{'name':'j','age':'3','height':'6feet'}
{'name':'r','age':'4','height':'5feet'}
{'name':'o','age':'5','height':'3feet'}
{'name':'n','age':'6','height':'4feet'}
{'name':'s','age':'7','height':'7feet'}
{'name':'q','age':'7','height':'8feet'}
I have gutted a lot of code to make this easier to read. The behaviour I would like is to print according to the number of lines specified. For now I will just use lines_to_execute=2. I would like to keep code as close as possible to minimize me rewriting this block. From this answer once working I will modify code so that it performs something chunks at a time.
Code block I want to stay close to:
Ill mix psudo code here as well
for majorkey in readerObj.keys():
lines_to_execute = 2
start_idx_position = 0
range_to_execute = lines_to_execute
for idx[start_idx_position:range_to_execute], line in enumerate(readerObj.get(majorkey)):
print line
increment start_idx_position by lines_to_execute
increment range_to_execute by lines_to_execute
time.sleep(1)
For this example if I want to print two lines or rows at a time, output would look like the below. Order is not important as same 2 don't get executed more than once:
Desired output:
{'name':'j','age':'3','height':'6feet'}
{'name':'r','age':'4','height':'5feet'}
One second delay...
{'name':'o','age':'5','height':'3feet'}
{'name':'n','age':'6','height':'4feet'}
One second delay.
{'name':'s','age':'7','height':'7feet'}
{'name':'q','age':'7','height':'8feet'}
I hope this is enough information to go on.
from pprint import pprint
import time
for key in obj.keys():
lines_to_execute = 2
pprint(obj[key][:lines_to_execute]) # that's all you need
time.sleep(1)
Keep it as simple as possible.

Creating a list from user input with swi-prolog

This is my first experience with Prolog. I am at the beginning stages of writing a program that will take input from the user (symptoms) and use that information to diagnose a disease. My initial thought was to create lists with the disease name at the head of the list and the symptoms in the tail. Then prompt the user for their symptoms and create a list with the user input. Then compare the list to see if the tails match. If the tails match then the head of the list I created would be the diagnosis. To start I scaled the program down to just three diseases which only have a few symptoms. Before I start comparing I need to build the tail of list with values read from the user but I can't seem to get the syntax correct.
This is what I have so far:
disease([flu,fever,chills,nausea]).
disease([cold,cough,runny-nose,sore-throat]).
disease([hungover,head-ache,nausea,fatigue]).
getSymptoms :-
write('enter symptoms'),nl,
read(Symptom),
New_Symptom = [Symptom],
append ([],[New_symptom],[[]|New_symptom]),
write('are their more symptoms? y or n '),
read('Answer'),
Answer =:= y
-> getSymptoms
; write([[]|New_symptom]).
The error occurs on the append line. Syntax Error: Operator Expected.
Any help with this error or the design of the program in general would be greatly appreciated.
This is one way to read a list of symptoms in:
getSymptoms([Symptom|List]):-
writeln('Enter Symptom:'),
read(Symptom),
dif(Symptom,stop),
getSymptoms(List).
getSymptoms([]).
You type stop. when you want to finish the list.
You would then need to decide what logic you want to match the way you have represented a disease.
A complete example:
:-dynamic symptom/1.
diagnose(Disease):-
retractall(symptom(_)),
getSymptoms(List),
forall(member(X,List),assertz(symptom(X))),
disease(Disease).
getSymptoms([Symptom|List]):-
writeln('Enter Symptom:'),
read(Symptom),
dif(Symptom,stop),
getSymptoms(List).
getSymptoms([]).
disease(flue):-
symptom(fever),
symptom(chills),
symptom(nausea).
disease(cold):-
symptom(cough),
symptom(runny_nose),
symptom(sore_throat).
disease(hungover):-
symptom(head_ache),
symptom(nausea),
symptom(fatigue).
create(L1):-read(Elem),create(Elem,L1).
create(-1,[]):-!.
create(Elem,[Elem|T]):-read(Nextel),create(Nextel,T).
go:- write('Creating a list'),nl,
write('Enter -1 to stop'),nl,
create(L),
write('List is:'),
write(L).

Prolog - sending a list as a parameter to be displayed

I'm trying to write a program to find a route between towns, add the path to the list and then, ad the end display it.
I think adding to the list works, but I'm having the problem displaying the list, don't know how can I pass a list as a parameter to be used when it's done finding the path? Hope you guys can help. Here's the code:
connected(middlesbrough, stockton).
connected(middlesbrough, darlington).
connected(stockton, sunderland).
connected(darlington, thirsk).
connected(stockton, newcastle).
connected(newcastle, york).
connected(thirsk, york).
connected(york, leeds).
connected(leeds, huddersfield).
connected(leeds, dewsbury).
connected(huddersfield, manchester).
connected(dewsbury, manchester).
run(List):-
write('Enter Starting City :'),
read(Start),
write('Enter Finishing City :'),
read(End),
findroute(Start,End),
writeList([List]).
findroute(Start,End):-
connected(Start,End).
findroute(Start,End):-
add(Start, List, [Start | List]),
connected(Start,Link), findroute(Link,End).
add(A,B,[A|B]).
writeList([]).
writeList([Head | Tail]):-
write(Head),
nl,
writeList(Tail).
Your findroute/2 predicate does not return the list, so the output can't work.
The call should look something like this:findroute(Start,End,List)
Obviously, the findroute/2 predicate must be changed to findroute/3:
findroute(Start,End,[Start,End]):-
connected(Start,End).
findroute(Start,End,List):-
connected(Start,Link),
add(Start,Rest,List),
findroute(Link,End,Rest).
(hint: be sure you understand why the add/3 call works even though Rest is uninstantiated at that point. Otherwise your tutor won't believe that this code is your homework! ;-) )
You may want to add a cut at the end of the first clause if you only want to find the shortest route.
Finally, List is already a list, so don't put square brackets around it when calling writeList/1!