Answer:
Following are the correct code to this question:
short_names=['Gus','Bob','Zoe']#defining a list short_names that holds string value
print (short_names[0])#print list first element value
print (short_names[1])#print list second element value
print (short_names[2])#print list third element value
Output:
Gus
Bob
Zoe
Explanation:
- In the above python program code, a list "short_names" list is declared, that holds three variable that is "Gus, Bob, and Zoe".
- In the next step, the print method is used that prints list element value.
- In this program, we use the list, which is similar to an array, and both elements index value starting from the 0, that's why in this code we print "0,1, and 2" element value.
Answer:
Following are the program in the Python Programming Language.
#declare empty string variable
s=""
#declare variable that store "*" as string
x= "*"
#set the while loop
while(len(s) < 777):
#initialize the value of 'x' in 's'
s= x
x += "*"
Explanation:
<u>Following are the description of the program</u>.
- Set an empty variable to store the string type values.
- Again, set a variable 'x' and initialize asterisks in it as a string.
- Set the while loop that iterate, when the length of the variable 's' is less than 777, then initialize the value of the variable 'x' in the variable 's' and concatenate asterisks with the variable 's'.
import java.util.Scanner;
public class JavaApplication86 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = 0, c = 0, sum = 0;
while (true){
x = scan.nextInt();
System.out.println("You entered: "+x);
if (x == -1){
break;
}
sum += x;
c++;
}
System.out.println("The sum is "+sum);
System.out.println("You entered "+c +" numbers");
}
}
This works for me. Instead of subtracting one from c and adding one to sum, I used an if statement to break away from the while loop if the entered number is -1.