-
Notifications
You must be signed in to change notification settings - Fork 2
Python Course Puzzles
Praveen Kumar Anwla edited this page Jan 27, 2024
·
7 revisions
Q1: Write a while/for loop that sums the values 1 through end, inclusive. end is a variable that we define for you. So, for example, if we define end to be 6, your code should print out the result: 21.
Ans: Using While loop:
end = 6 # You can change this value to any desired positive integer
# Initialize variables
sum_result = 0
current_value = 1
# While loop to sum values
while current_value <= end:
sum_result += current_value
current_value += 1
# Print the result
print(sum_result)
end = 6 # You can change this value to any desired positive integer
# Initialize variables
sum_result = 0
current_value = 1
# While loop to sum values
for i in range(1, end+1):
if i <= end:
sum_result += i
i += 1
# Print the result
print(sum_result)