print() prints only every second input
You are calling inputFilterText twice. Once within the print() and once before. This is causing the code to prompt for input twice before printing the second response.
Python read whatever is entered into console every second
If I understand correctly the problem is that you can't read inputs without the user pressing enter.
If you are on windows I would use msvcrt
It should be preinstalled
so the code would look a bit like
import msvcrt
while gameloop: #just the gameloop
# your code and all
input_char = msvcrt.getch(); #as a not this does return a char rather then a string
if (input_char.lower() == "some letter or number"):#do stuff...
Printing a character each second [closed]
well, I'm guessing you want to get a total time from the user and print a character with a time interval of 1 second that adds up to the total time?
if that's the case here's the code,
import time #this module helps us use the cpu's time in our python programe
time_limt = int(input("enter your total time: ")) #make sure you dont name this variable as time as it would mess up the refrences
char = str(input("enter the chracter you want to print: "))
for i in range(time_limit): #this is for printing up to the time limit
time.sleep(1) #this stops the programe for 1 second
print(char)```
Trying to print every second line of a file in C
Do not use strcat()
on an uninitialised char
-array.
char string[1000];
char evenString[1000];
...
strcat(string, line);
...
strcat(evenString, line);
as it expects valid "string"s as arguments. An uninitialised char
-array is not a string. In C a string needs to have at least one char
being set to '\0'
to mark its end. This is also called the string's 0
-terminator, or NUL
-terminator (note the one ell only), or null-terminator (note lower cases and the two ells).
Initialise the char
-array properly to become an empty string.
char string[1000] = "";
char evenString[1000] = "";
Or more obscure you could do
string[0] = '\0';
or
strcpy(string, "");
Also you really want to test whether fopen()
failed:
FILE *fptr = fopen(fileName, "r");
if (NULL == fptr)
{
perror("fopen() failed");
exit(EXIT_FAILURE); /* include stdlib.h for EXIT_xxx macros. */
}
Another subtle pitfall you have here in case more is read then can be store:
for(y = 1; y < lineCounter; y++){
fgets(line, sizeof(line), fptr);
if(y % 2 == 0){
strcat(evenString, line);
}
}
This can be fixed in several ways. A straight forward approach would be:
for(y = 1; y < lineCounter; y++){
fgets(line, sizeof(line), fptr);
if(y % 2 == 0){
if (sizeof evenString -1 < strlen(evenString) + strlen(line)
/* -1 to take into account the space needed
for string's '0'-terminator. */
{
errno = ERANGE; /* include errno.h for errno and Exxx macros. */
perror("evenString to short for another line");
exit(EXIT_FAILURE);
}
strcat(evenString, line);
}
}
Same for case 'n'
.
IndexError: list index out of range prints out only every second row in python
The issue with your code is that you're doing for line in f:
but also header1 = f.readline()
. The for line in f:
goes through each line, but the header1 = f.readline()
removes the next line (and saves it into header1
). All you have to do is remove the header1 = f.readline()
.
You can also be a little less wordy in the code by doing
with open("path/mytextfile.txt") as f:
for line in f:
columns = line.strip().split()
if 'str' in line:
break
print(columns[5])
but it doesn't really matter.
I believe the index error is happening because the for line in f:
finds out how many iterations it needs to go through, but f
is changing size. It's generally a bad idea to change the size of a list
or file as you're going through it.
Related Topics
How to Find a Minimum Value in a 2D Array Without Using Numpy or Flattened in Python
How to Convert Data from Txt Files to Excel Files Using Python
Reading a CSV File into Pandas Dataframe With Quotation in Some Entries
Easiest Way to Convert Two Columns to Python Dictionary
How to Name a File by a Variable Name in Python
Can Anyone Explain Me What This Python 3 Command Do
Importing Local Module (Python Script) in Airflow Dag
Copy All Values in a Column to a New Column in a Pandas Dataframe
How to Check If a String Column in Pyspark Dataframe Is All Numeric
How to Find a Word That Starts With a Specific Character
Selecting Specific Rows and Columns from Numpy Array
Finding the Index of the First Occurrence of Any Item in a List
Identifying the Range of a Color in Hsv Using Opencv
How to Get the Sum of a CSV Column List to Print
Python Ttk Treeview: How to Select and Set Focus on a Row
Pandas - Find Rows With Matching Values in Two Columns and Multiply Value in Another Column