How to ask for multiple lines of input and print them out as a list in Python - input

How do I write a code which prints out multiple lines of input and prints it back as a line? The inputs should look something like this
Enter participant 1: Jack
Enter participant 2: Jill
…
Enter participant 6: Joshua
lanes = int(5)
inputs = []
for x in range(loops):
inputs.append(input("Enter participant ", x))
I went all the way till here to store the values but wasnt sure how to print a different line each time

Python inputs takes in one argument, and you can loop it for as many participants as you want.
inputs = []
for x in range(1, 7) #starts from 1 and loops till 7:
inputs.append(input("Enter participant " + str(x) + ": "))
print(inputs)

Related

How can I delete sub-sub-list elements based on condition?

I am having the following two (2) lists:
lines = [[[0, 98]], [[64], [1,65,69]]]
stations = [[0,1], [0,3,1]]
The lines describes the line combinations for getting from 0 to 1 and stations describes the stops visited by choosing the line combinations. For getting from 0 to 1 the following are the possible combinations:
Take line 0 or 98 (direct connection)
Take line 64 and then line 1 (1 transfer at station 3)
Take line 64 and then line 65 (1 transfer at station 3)
Take line 64 and then line 69 (1 transfer at station 3)
The len of stations always equals the len of lines. I have used the following code to explode the lists in the way I described previously and store the options in a dataframe df.
result_direct = []
df = pd.DataFrame(columns=["lines", "stations", 'transfers'])
result_transfer = []
for index,(line,station) in enumerate(zip(lines,stations)):
#print(index,line,station)
if len(line) == 1: #if the line store direct connections
result_direct = [[i] for i in line[0]] #stores the direct connections in a list
for sublist in result_direct:
df = df.append({'lines': sublist,'stations': station, 'transfers': 0},ignore_index=True)
else:
result_transfer = [[[x] for x in tup] for tup in itertools.product(*line)]
result_transfer = [[item[0] for item in sublist] for sublist in result_transfer]
for sublist in result_transfer:
df = df.append({'lines': sublist,'stations': station, 'transfers': len(sublist)-1},ignore_index=True)
For the sake of the example I add the following 2 columns score1, score2:
df['score1'] = [5,5,5,2,2]
df['score2'] = [2,6,4,3,3]
I want to update the values of lines and stations based on a condition. When score2 > score1 this line/station combinations should be removed from the lists.
In this example the direct line 98 as well as the combination of lines 64,65 and 64,69 should be removed from the lines. Therefore, the expected output is the following:
lines = [[[0]], [[64], [1]]]
stations = [[0,1], [0,3,1]]
At this point, I should note that stations is not affected since there is at least one remaining combination of lines in the sublists. If also line 0 should have been removed the expected output should be:
lines = [[[64], [1]]]
stations = [[0,3,1]]
For starting I have tried a manual solution that works for a single line removal (e.g for removing line 98):
lines = [[y for y in x if y != 98] if isinstance(x, list) else x for x in [y for x in lines for y in x]]
I am having the difficulty for line combinations (e.g 64,65 and 64,69). Do you have any suggestions?

Lua - It is possible to stop inputs while "ex.sleep" is running?

Basic stuff that I can't figure out or find in internet:
The little code I'm using for tests is simple:
require("ex")
a = true
b = nil
while (a == true) do
b = io.read()
ex.sleep(5)
print(b)
end
Very simple. If I input "1" (I am using notepad++ and windows command prompt), it will wait 5 seconds and print it, then repeat. But my problem is... If I input more numbers during the 5 seconds of sleeping, it all will be executed automatically, in order, when the sleep ends.
Is it possible to stop that? I don't want any input being read during that time. Where these "ghost" inputs are stored?
You can control reading by means of "buffer size" argument in bytes:
b = io.read(1)
In this case reading completes after the first byte was taken from input. Rest input bytes will be available for the next "read" statement.
Important note: if you input "1" and press "Enter" then there will be 3 bytes for reading (including "\r\n").
See https://www.lua.org/pil/21.1.html for details.
In addition, you want to know a way to clean input buffer before next reading. This is easy: use io.read("*line") statement as follows:
b = io.read("*line") -- suppose, input is: "1234"
b = string.sub(b, 0, 1)
print(b) -- prints 1
b = io.read("*line") -- suppose, input is: "567"
b = string.sub(b, 0, 1)
print(b) -- prints 5
b = io.read("*line") -- suppose, input is: ""
b = string.sub(b, 0, 1)
print(b) -- prints empty string
io.read("*line") gets whole line from input, but you can take only the first character from it.

I would like to create an auto-schedule but I have some problems

Creating a schedule where I introduce a word and the program returns the info I need is what I want. I mean, if I write the word "monday" I would like a response with the subjects I have that day. I did this (very brief example, I have more subjects):
x = int(input("Day of the week: "))
if x == 2:
x = 0
print('9:00-11:00 Biology - Classroom C4B \n11:00-13:00 Maths- Classroom C5')
elif x == 3:
print('11:00-13:00 Physics - Classroom C4B')
This works, but the problem is that I do not want to enter numbers but words. I also tried with eval and works too. However, in that case, I must enter a word between '' because eval reads strings and that is not what I want. How can I improve my program?
Thanks in advance (Python 3)
And because you do not use raw_input( )?
input() actually evaluates the input as Python code.
And in your code x = int(input("Day of the week: ")) only accepts numerics inputs. raw_input() returns the verbatim string entered by the user.
day = raw_input("Day of the week: ")
if day == "monday":
print('9:00-11:00 Biology - Classroom C4B \n11:00-13:00 Maths- Classroom C5')
elif day == "tuesday":
print('11:00-13:00 Physics - Classroom C4B')

Checking errors in my program

I'm trying to make some changes to my dictionary counter in python. I want make some changes to my current counter, but not making any progress so far. I want my code to show the number of different words.
This is what I have so far:
# import sys module in order to access command line arguments later
import sys
# create an empty dictionary
dicWordCount = {}
# read all words from the file and put them into
#'dicWordCount' one by one,
# then count the occurance of each word
you can use the Count function from collections lib:
from collections import Counter
q = Counter(fileSource.read().split())
total = sum(q.values())
First, your first problem, add a variable for the word count and one for the different words. So wordCount = 0 and differentWords = 0. In the loop for your file reading put wordCount += 1 at the top, and in your first if statement put differentWords += 1. You can print these variables out at the end of the program as well.
The second problem, in your printing, add the if statement, if len(strKey)>4:.
If you want a full example code here it is.
import sys
fileSource = open(sys.argv[1], "rt")
dicWordCount = {}
wordCount = 0
differentWords = 0
for strWord in fileSource.read().split():
wordCount += 1
if strWord not in dicWordCount:
dicWordCount[strWord] = 1
differentWords += 1
else:
dicWordCount[strWord] += 1
for strKey in sorted(dicWordCount, key=dicWordCount.get, reverse=True):
if len(strKey) > 4: # if the words length is greater than four.
print(strKey, dicWordCount[strKey])
print("Total words: %s\nDifferent Words: %s" % (wordCount, differentWords))
For your first qs, you can use set to help you count the number of different words. (Assume there is a space between every two words)
str = 'apple boy cat dog elephant fox'
different_word_count = len(set(str.split(' ')))
For your second qs, using a dictionary to help you record the word_count is ok.
How about this?
#gives unique words count
unique_words = len(dicWordCount)
total_words = 0
for k, v in dicWordCount.items():
total_words += v
#gives total word count
print(total_words)
You don't need a separate variable for counting word counts since you're using dictionary, and to count the total words, you just need to add the values of the keys(which are just counts)

how to use less condition for many condition?

i mean for e.g., numbers are between 1 and 100 .i want to show messagebox for each number as text for example "One" for 1.
is it possible ?
anyone help!
I wrote a similar program a few years ago. Create a String array with One, Two..Twenty, Thirty, Forty...Hundred
Then with the input number check if <20 just print that array index.
if(num < 20)
print arr[num]
if num>20
print arr[num/10 + 19] + " " + arr[num%10]
I hope you get the drift....