Making a Python3 Variable Using a Counter [closed] - bash

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have some BASH code that I want to convert to Python3.
INPUTINDEX$INPUTCOUNTER="$INPUT"
Every time a user types an input the counter (code not shown) increases one number. With this code, the user's second input would be in variable INPUTINDEX2. INPUT, of course, is the user's input. The INPUTCOUNTER is a counter that increases each time an input is entered.
The question: How can I do this in Python3.3?

I suggest just appending to a list and the number of entries in the list is your counter.
#!/usr/bin/env python
def main():
entries=[]
while True:
entries.append(raw_input('input something: '))
if not len(entries[-1]):
entries=entries[:-1]
break
counter=len(entries)
print 'counter %d, entries %s.' % (counter, entries)
if (__name__ == "__main__"):
main()

Related

How I get 2 values ​in one line on ruby? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to get two value in one line without split function. Help me to solving this problem. Thank you!
If you want to assign 2 variables in one line you can do
def multiple_values
return 1, 2
end
x, y = multiple_values
# x = 1
# y = 2

I want to choose specific characters from a variable in Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I capture the text value and store it in a variable. Now I want to use the specific characters from the variable. How can I do it in Ruby?
var = "ABC-DEF-XYZ"
I want to use "DEF" value from the variable and want to skip the rest.
You can capture the 'y' value from your string using one of these 2 methods:
Regex
y = input_value.gsub(/^\w\w\w-(\w\w\w)-\w\w\w$/, "\\1")
Split
y = input_value.split('-')[1]
Once you have the 'y' value from your input, you can compare it to the table list.

Something similar? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm in my first year and I have to do some problems in a new programming language invented by my teacher which is very similar with java
The problem is: Write a program that asks the user to enter a number. It will then repeatedly display the message "hello" twice the number of times as the number entered. So if you entered 3 it should print "hello" 6 times.
So far I did this:
get number
set p=number*2
for p=1 to 100
repeat
display "hello"
until number !=p
endfor
What am I doing wrong ?
get number
set p=number*2
for i=1 to p
display "hello"
endfor

How to ask a number in Ruby? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to make a program like,ask a number and print 1 to number by using gets and by using loop. So,I am asking about gets and how to do program which is I given below as program title.
How to ask a number by using gets?If possible explain me with example.
By using gets,I want to print 1 to number. My program titleis Ask a number and print 1 to number by using Ruby.
How can I solve that program?Please help me on this.
As Arup, suggested use Kernel#gets to capture a user input from terminal. The remaining bit can be simply done with a for loop:
num = gets.to_i #Convert the user input to integer
for i in 1..num
puts i
end
You can further modify this to suit your need.
Do as below using Kernel#gets. #gets will give you a string, then to convert the number string to a number use String#to_i.
number = gets.to_i
If I want to make program which from 1 to number then what should I do?
Use a Range then.
(1..number).each do |n|
# code
end

How to compute the number of char, line by line [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a Ruby program that, given a file as parameter, returns a hash or array that gives the number of characters for each line.
How can I do this elegantly in Ruby ?
File.open('file_name').map(&:length)
Check this:
File.open('file_name').inject([]) do |counts, line|
counts << line.size
end
Take a note that it will also count new line characters.
For file with content
aa
aaaa
a
the result will be
[3, 5, 1]
If you don't want to count them, check this method String#chomp

Resources