Hello good day guys. I just want to ask for help in my java python. I am just new in this language.
MY first java language is java. However, for me to make it easier to code in python, i need to code it in java first and then convert after. But i am confused working with arrays in python, help me guys. Thank you.

import java.util.*;

public class sample {
	static Scanner scan = new Scanner(System.in);
	
	public static void main (String args[]){
		sample();
	
	}
	public static void sample(){
		
		System.out.println("Enter numbers : ");
		String input[] = new String[5];
		for ( int i = 0 ; i < input.length; i++){
			
			input[i] = scan.nextLine();
			
		}
		for ( int i = 0 ; i < input.length; i++){	
			System.out.println(input[i]);
		}
	}
}

Recommended Answers

All 3 Replies

Which version of Python are you using?

Using Python2 you would use something like this to create an array of names. Generally Python uses lists for arrays.

# create the name list
name_list = []
while True:
    name = raw_input("Enter your name (q to quit): ")
    if name.lower() == 'q':
        break
    name_list.append(name)

# show the name_list contents
for name in name_list:
    print name

With numbers you can do something similar ...

# create the number list (int or float)
number_list = []
while True:
    number = input("Enter a number (-99 to quit): ")
    if number == -99:
        break
    number_list.append(number)

# show the number_list contents
for number in number_list:
    print number

If you want to enter a fixed number of numbers, use a for loop ...

# create the number list (int or float)
number_list = []
for k in range(5):
    number = input("%d) Enter a number : " % (k+1))
    number_list.append(number)

# show the number_list
for number in number_list:
    print number

hmm..thanks for the reply. :)
i figured it out. :)

So, what is your solution and which version of Python are you using?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.