30 likes | 152 Vues
This guide breaks down two practice exercises in Python. The first exercise involves prompting the user for a text input and printing each character with its index. The second exercise calculates the sum of integers from 1 to n, where n is provided by the user. It emphasizes understanding user input and loops, including a for loop and a while loop for summation, enhancing problem-solving skills in Python programming.
E N D
Practice 1 • What does this code do? message = input("Please enter text: ") for i in range(0, len(message), 1): print i, ":", message[i]
Practice 2 • Write a code to calculate 1+2+3+….+n (where n is a number that the user inputs)
Solution n = int(raw_input("enter a number: ")) add = 0 for num in range(n + 1): add += num #write this loop using a while loop print "the sum is", add