In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how python uses while to find integers within 100. the article is very detailed and has a certain reference value. Interested friends must read it!
The sum of 1, 1 to 100
Define two variables I and sum, the initial value is 1, the value of I increases by 1 at a time, and after taking 100, the program ends, and the value of sum is equal to the value of itself plus I. So I is taken from 2 to 100 and is added to sum every time.
#! / usr/bin/env python#-*- coding:utf-8-*-i=1sum=1while True: the sum of even numbers in breakprint (sum) 2, 1 to 100
Method 1: same as above, except that the initial value of iMagol sum is 0 and the value of I increases by 2 each time, and the program ends after taking 100.
#! / usr/bin/env python#-*- coding:utf-8-*-i=0sum=0while True: breakprint (sum)
Method 2: by taking the remainder function%, let num%2, if equal to 0, be an even number, sum=sum+num
#! / usr/bin/env python#-*-coding:utf-8-*-# Python Learning Exchange Group: 778463939num=0sum=0while True: num+=1 if num%2==0: sum=sum+num if num==100: breakprint ("Task finishedship the sum of even numbers from 1 to 100is:" + str (sum))
Little knowledge: the equal sign is =, which can no longer be written as num%2=0.
3. Odd sum from 1 to 100
Method 1: same as above, except that the initial value of iMagol sum is 1 and the value of I increases by 2 at a time. Here, we should pay special attention to the end of the program after taking the value of I to 99, otherwise the program will end.
#! / usr/bin/env python#-*- coding:utf-8-*-i=int (1) sum=int (1) while True: iTunes 2 sum=sum+i if iTunes 99: breakprint (sum)
Method 2: by taking the remainder function%, let num%2, if equal to 1, be odd, sum=sum+num
#! / usr/bin/env python#-*-coding:utf-8-*-num=0sum=0while True: num+=1 if num%2==1: sum=sum+num if num==100: breakprint ("Task finding the sum of odd numbers from 1 to 100is:" + str (sum)) these are all the contents of the article "how python uses while to find integers within 100". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.