1 / 3

Understanding Basic Python Code: Input Processing and Summation Logic

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.

belita
Télécharger la présentation

Understanding Basic Python Code: Input Processing and Summation Logic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Practice 1 • What does this code do? message = input("Please enter text: ") for i in range(0, len(message), 1): print i, ":", message[i]

  2. Practice 2 • Write a code to calculate 1+2+3+….+n (where n is a number that the user inputs)

  3. 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

More Related