Python 3.5 如何实现这个while循环或for循环
Python 3.5 how to implement this while loop or for loop
我是编程新手,正在学习 python。我很高兴地宣布,到目前为止,我已经编写了 3 个短程序。我正在试验 while 和 for 循环,但都无法在我的程序中实现。这是我第一次尝试使用 for 循环的伪代码。
声明计数
运行闰年计划3
计数 + 1 = 计数
返回第 2 行
我很欣赏有多种方法可以做到这一点(range、xrange、while 等),我试图了解 for 循环的工作原理以及如何通过以下代码实现它。
# My leap year Python program.
# This program demonstrates the following:
# Determines if a user input of year is a leap year using the calendar
# function
#
# Author: Thomas Schuhriemen
# Course: CIS1400
import calendar
t1= int(input())
print ("Welcome to my leap year program")
print ("You may type any year and I will tell you if it is a leap year")
# some of the following is based on code written by INDIAN VILLAN on
# comments on w3resource.com
# 3.0 is a stable build but this one (4.1) is experimenting with for to
# repeat the question.
count = 0
for count >= 5:
if calendar.isleap(t1) is True:
print (t1, "is a leap year.")
else:
print (t1, "is not a leap year.")
count = count + 1
我似乎无法理解为什么这总是给我错误。我给了我一个错误,说紧接着 for 命令的代码有问题它说 "for count >= 5 & count <= 0:" 的代码有一个无效的语法错误突出显示
"="
感谢您有兴趣帮助我学习如何使用 for!
托马斯。不用担心旅程开始时的严酷尝试。我建议你看一下 Python 官方文档中的初学者教程 The Python Tutorial。 Here is the link to the explanation of the for loop.
现在,请记住这一点:编程语言(事实上,即使是人类语言)最基本的概念之一就是语法。这意味着一切在句子中都有其应有的位置,因此可以从这句话中提取含义。
在Python中,for循环的基本语法如下:
for [variable] in [collection of things]:
[block of code inside the loop]
我放在括号内的所有内容你都可以修改。
请注意单词 for
和 in
(以及末尾的冒号)是强制性的。您必须将它们保留在此设置中。
正如我所说,请按照自己的节奏从头到尾查看 Python 教程。它将通过最好的信息来源:官方 Python 文档帮助您学习 Python。
for循环语法错误,这里不需要使用。参考 Python Wiki here
import calendar
print ("Welcome to my leap year program")
print ("You may type any year and I will tell you if it is a leap year")
count = 0
while count <= 5:
t1= int(input())
if calendar.isleap(t1): # returns true if t1 is leap year else returns false
print (t1, "is a leap year.") # if test condition is true
else:
print (t1, "is not a leap year.") # if test condition is false
count = count + 1
查看实际效果 here
import calendar
print ("Welcome to my leap year program")
print ("You may type any year and I will tell you if it is a leap year")
count = 0
for i in range(5):
count = count + i
t1= int(input())
if calendar.isleap(t1): # returns true if t1 is leap year else returns
false
print (t1, "is a leap year.") # if test condition is true
else:
print (t1, "is not a leap year.") # if test condition is false
我学会了正确的语法并且能够想出这个。感谢所有发布学习建议的人。
导入日历
打印("Welcome to my leap year program")
打印 ("You may type any year and I will tell you if it is a leap year")
count = 0
for i in range(5):
count = count + i
t1= int(input())
if calendar.isleap(t1): # returns true if t1 is leap year else returns
false
print (t1, "is a leap year.") # if test condition is true
else:
print (t1, "is not a leap year.") # if test condition is false
我是编程新手,正在学习 python。我很高兴地宣布,到目前为止,我已经编写了 3 个短程序。我正在试验 while 和 for 循环,但都无法在我的程序中实现。这是我第一次尝试使用 for 循环的伪代码。
声明计数
运行闰年计划3
计数 + 1 = 计数
返回第 2 行
我很欣赏有多种方法可以做到这一点(range、xrange、while 等),我试图了解 for 循环的工作原理以及如何通过以下代码实现它。
# My leap year Python program.
# This program demonstrates the following:
# Determines if a user input of year is a leap year using the calendar
# function
#
# Author: Thomas Schuhriemen
# Course: CIS1400
import calendar
t1= int(input())
print ("Welcome to my leap year program")
print ("You may type any year and I will tell you if it is a leap year")
# some of the following is based on code written by INDIAN VILLAN on
# comments on w3resource.com
# 3.0 is a stable build but this one (4.1) is experimenting with for to
# repeat the question.
count = 0
for count >= 5:
if calendar.isleap(t1) is True:
print (t1, "is a leap year.")
else:
print (t1, "is not a leap year.")
count = count + 1
我似乎无法理解为什么这总是给我错误。我给了我一个错误,说紧接着 for 命令的代码有问题它说 "for count >= 5 & count <= 0:" 的代码有一个无效的语法错误突出显示 "="
感谢您有兴趣帮助我学习如何使用 for!
托马斯。不用担心旅程开始时的严酷尝试。我建议你看一下 Python 官方文档中的初学者教程 The Python Tutorial。 Here is the link to the explanation of the for loop.
现在,请记住这一点:编程语言(事实上,即使是人类语言)最基本的概念之一就是语法。这意味着一切在句子中都有其应有的位置,因此可以从这句话中提取含义。
在Python中,for循环的基本语法如下:
for [variable] in [collection of things]:
[block of code inside the loop]
我放在括号内的所有内容你都可以修改。
请注意单词 for
和 in
(以及末尾的冒号)是强制性的。您必须将它们保留在此设置中。
正如我所说,请按照自己的节奏从头到尾查看 Python 教程。它将通过最好的信息来源:官方 Python 文档帮助您学习 Python。
for循环语法错误,这里不需要使用。参考 Python Wiki here
import calendar
print ("Welcome to my leap year program")
print ("You may type any year and I will tell you if it is a leap year")
count = 0
while count <= 5:
t1= int(input())
if calendar.isleap(t1): # returns true if t1 is leap year else returns false
print (t1, "is a leap year.") # if test condition is true
else:
print (t1, "is not a leap year.") # if test condition is false
count = count + 1
查看实际效果 here
import calendar
print ("Welcome to my leap year program")
print ("You may type any year and I will tell you if it is a leap year")
count = 0
for i in range(5):
count = count + i
t1= int(input())
if calendar.isleap(t1): # returns true if t1 is leap year else returns
false
print (t1, "is a leap year.") # if test condition is true
else:
print (t1, "is not a leap year.") # if test condition is false
我学会了正确的语法并且能够想出这个。感谢所有发布学习建议的人。
导入日历 打印("Welcome to my leap year program") 打印 ("You may type any year and I will tell you if it is a leap year")
count = 0
for i in range(5):
count = count + i
t1= int(input())
if calendar.isleap(t1): # returns true if t1 is leap year else returns
false
print (t1, "is a leap year.") # if test condition is true
else:
print (t1, "is not a leap year.") # if test condition is false