Python: printing every other input using a for loop

Click For Summary
SUMMARY

The discussion focuses on a Python program designed to print every other input from a sequence of integers and strings. The user attempts to implement this using a for loop and the input function, but encounters issues with the expected output format. The solution involves correctly alternating between reading integers and strings, ensuring that only integers are printed based on the first input indicating the total number of strings. The provided code snippet demonstrates the logic but requires adjustments to achieve the desired output.

PREREQUISITES
  • Understanding of Python input handling
  • Familiarity with for loops in Python
  • Knowledge of exception handling in Python
  • Basic understanding of string manipulation and the isdigit() function
NEXT STEPS
  • Learn about Python exception handling for input validation
  • Explore the use of the isdigit() function for string validation in Python
  • Research Python's input() function and its behavior with different data types
  • Study examples of alternating input handling in Python programs
USEFUL FOR

Python developers, students learning programming concepts, and anyone interested in handling user input effectively in Python applications.

Sunwoo Bae
Messages
60
Reaction score
4
I want my output to read only the integers. Here's my attempt:

number = int(input())

for i in range(2, number+1, 2):
print(input())
 
Technology news on Phys.org
Here's some code that illustrates what you are trying to do. Basically if the int(some_string) fails to extract a number an exception is thrown.

https://pynative.com/python-check-user-input-is-number-or-string/

Alternatively, you could parse the string into characters and use the isdigit() function to see if all are digits or '.' or - or + ... depending on what you define as an acceptable number.
 
Sunwoo Bae said:
Homework Statement: I have to print every other input.
Homework Equations: you are not given input to put in or the total number of input, but the input shows a total number of strings in the first line and the rest consists of alternation of string and integer.
for example, the input can be:

3
12
tiger
11
cats
10
dogs

I want my output to read only the integers. Here's my attempt:

number = int(input())

for i in range(2, number+1, 2):
print(input())
The first number indicates the number of strings in the sequence of inputs. It should be used to control a for loop that runs that many times, with pairs of input calls. For example, in your sample inputs, the first number entered indicates that three strings will be in the input sequence. Because the rest of the input values alternate between integers and strings, the loop body should do two inputs: the first to input a number and the second to input a string but not do anything with it.

Notice that the problem statement says "the rest consists of alternation of string and integer." but the sample input sequence has this alternation in the opposite order. That is, as integer, string, integer, string, and so on.

I wrote a short program that appears to satisfy the program requirements, using exactly the sample inputs you wrote. Here is the output from that program.
Code:
Number of strings: 3
12
Val:  12
tiger
11
Val:  11
cats
10
Val:  10
dogs
The only output from my program are the initial prompt to enter the first number and the lines that start with Val: ...
All other lines are what I typed as inputs to the program.
 

Similar threads

Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
5
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
3
Views
2K