Code, Python
Code, Python
2.
3. I’ve start by putting a variable ‘althea’ to 1. Next, a while loop to continue to execute as long
as the condition count <= 10 is true. The count will stop if it reaches to 10. Then, placed the print
to print the current value then used the althea = althea + 1 in order to go to the next number in
the sequence and prevent an infinite loop.
4. althea = 1
while althea <=10:
print(althea)
althea = althea + 1
Activity #2 Print “Hello World”
1.
2.
3. I’ve start by putting a variable ‘ballpen’ to 0. Next, placed a while loop condition to count < 5
which is true, the while loop will keep running. Then, put print and inside the loop there’s a
statement “Hello World” to print. Lastly, ballpen = ballpen + 1 in order to control the amount of
times the loop will run and to prevent an infinite loop.
4. ballpen = 1
3. I’ve start by putting two variables ‘sum_result’ to 0 and ‘current_number ‘ to 1. Next, while
loop continues as long as count is less than or equal to 100. Then, sum_result = sum_result +
current_number is to add the current value of current_number to the running sum while
current_number = current_number + 1 is to move to the next number by adding 1 to
current_number. Lastly, print to print the final sum of numbers from 1 to 100 stored in
‘sum_result’.
4. sum_result = 0
current_number = 1
3. Start to user input, ask the user for a number, then verify if it's prime. Next, check the
number's divisibility starting at divisor = 2 and assuming it is prime (is_prime = True). As long
as divisor is less than the input integer, keep testing divisibility. Verify whether the input integer
is divisible by the current divisor inside the loop then srtet is_prime to False if it is. To test
divisibility with the next integer, increment the divisor by 1 for each iteration. Check whether
is_prime is still True and the input integer is greater than 1 after the loop. Lastly, print the
number is prime if this is true; print number it is not prime if this is false.
4.
number = int(input("Enter a number if it is a prime number"))
is_prime = True
divisor = 2
divisor = divisor + 1
3. Start in setting the count and total variables to zero. The count of the numbers and the total
number of inputs will be counted using these variables. Print a message telling the user to submit
numbers so that the average can be determined. To end, the user can input 0. Take a number as
input from the user inside the loop. Make the input from the user a float. Verify that the number
entered is 0. If so, exit the loop so the user can complete entering numbers. In case the number
entered is not zero, add it to the total and move the count up by one. Check to see whether no
numbers were entered (count == 0) after the loop. If this is the case, print a notice stating that the
average cannot be calculated since no numbers were entered. Lastly, if input data was entered,
compute the average by splitting the total by the number of entries (average = total / count) and
output the outcome.
4.
total = 0
count = 0
number = float(user_input)
if number ==0:
break
if count == 0:
print("No numbers entered. Cannor calculate average.")
else:
average = total/ count
print ("Average:", average)
2.
3. Start in placing a variable “my_list” with an empty list so that it will store all the number that I
will put in it. Then, use while loop len(my_list) < num_elements. This loop continues until the
length of my_list reaches the specified num_elements which is 5. Ask the user to enter a number
inside the loop, then turn that number into a float and save it in the new_element variable. Use
the add method to append the recently inserted element (new_element) to the my_list. The loop
keeps asking the user and adding items to the list until the list's length equals num_elements.
Lastly, print the final content of the list using print("Final list:", my_list)
4.
my_list = []
num_elements = 5
my_list.append(new_element)
2.
3. Start by placing a variable ‘my_list’ with a list of random numbers. Then set the threshold
value of 20. Set index to 0 and legth to len(my_list). Employ a while loop where the length is the
condition index. The elements of my_list are iterated through in this loop. Check whether the
element in my_list at the current index is larger than the threshold inside the loop. If the element
exceeds the threshold, use del my_list[index] to remove the element at the current index and
update the length to reflect the new length of the updated list. To go to the next element in the
list, increase the index by 1 if the element is not larger than the threshold. Until the index is no
longer smaller than the list's length, the loop iterates continuously. Lastly, print the last part.
4.
my_list = [17, 25, 37, 18, 11, 56, 74]
threshold = 20
index = 0
length = len(my_list)
del my_list[index]
length = len(my_list)
else:
index = index + 1