计算相邻奇数的个数
Calculating number of adjacent odd numbers
我正在尝试在 Python 中编写代码,要求用户输入序列中的数字个数,然后是数字本身。最后,程序输出相邻奇数对的数量。这是示例输出:
输入序列长度:6
输入数字 1: 3
输入数字 2: 4
输入数字 3: 7
输入数字 4: 9
输入数字 5: 3
输入数字 6: 5
相邻奇数的对数为3
我想出了以下代码:
length = eval(input("Enter the length of the sequence: "))
for i in range(1,length+1):
ask = eval(input("Enter number: "+str(i)+ ": "))
for m in range(0,length+1,2):
ask2 = ask
h = ask%2
f = ask2%2
if h>0 and f>0:
k = (len(str(ask) + str(ask2)))
print(k)
else:
pass
虽然提示的输出是正确的,但我无法计算相邻奇数对的数量。请帮助我更正我的代码或在此基础上进行构建;这将不胜感激。您一定已经注意到,我一直在使用基本的 if 语句、循环和字符串来编写代码。如果你能坚持这个让我更好地理解,那就太好了。
抱歉这么久 post。
非常感谢
检查当前元素和下一个元素是否都是奇数和和:
length = int(input("Enter the length of the sequence: "))
nums = [int(input("Enter number: {}: ".format(i))) for i in range(1, length + 1)]
print(sum(ele % 2 and nums[i] % 2 for i,ele in enumerate(nums, 1)))
enumerate(nums, 1)
从 1
开始索引,因此 ele % 2 and nums[i] % 2
在我们用下一个相邻数字遍历 nums 时检查当前元素。
当你想转换为 int 时使用 int(input..
,使用 eval 永远不是一个好主意。您还应该使用 while
循环并使用 try/except
.
验证用户输入
不使用列表:
length = int(input("Enter the length of the sequence: "))
total = 0
# get a starting number
ask = int(input("Enter number: {}".format(1)))
# will keep track of previous number after first iteration
prev = ask
for i in range(2, length + 1):
ask = int(input("Enter number: {}".format(i)))
# if current and previous are both odd increase count
if ask % 2 and prev % 2:
total += 1
# update prev
prev = ask
print(total)
这是一种使用简单 for
循环的方法。就像 Padraic 的解决方案一样,它使用列表来累积输入,因为它比在读入时尝试测试对更简单。正如 Padraic 提到的,你真的应该检查输入是否正确。
length = int(input("Enter the length of the sequence: "))
seq = []
for i in range(1, length + 1):
ask = int(input("Enter number " + str(i) + ": "))
seq.append(ask)
count = 0
for i in range(length - 1):
if seq[i] % 2 == 1 and seq[i+1] % 2 == 1:
count += 1
print("The number of pairs of adjacent odd numbers is", count)
这将解决您的问题。
首先将用户给出的输入放入名为numList的列表中。保留一个计数变量来统计相邻奇数的个数。一个一个循环遍历numList,通过检查除以2的余数来识别奇数。(通过给定的if条件检查)然后您可以简单地打印列表中相邻奇数的数量。
length=int(input("Enter the length of the sequence: "))
numList=[]
count=0
for i in range(length):
num=int(input("Enter number "+str(i+1)+ ": "))
numList.append(num)
for x in range(len(numList)-1):
num1=numList[x]
num2=numList[x+1]
if((num1%2==1) and (num2%2==1)):
count=count+1
else:
continue
print("The number of pairs of adjacent odd numbers is "+str(count))
如果您想在不使用列表的情况下解决这个问题,这就是答案。
您应该在接收输入时处理输入。
length=int(input("Enter the length of the sequence: "))
count=0
num1=int(input("Enter number "+str(1)+ ": "))
for i in range(length-1):
num2=int(input("Enter number "+str(i+2)+ ": "))
if((num1%2==1) and (num2%2==1)):
count=count+1
num1=num2
print("The number of pairs of adjacent odd numbers is "+str(count))
我正在尝试在 Python 中编写代码,要求用户输入序列中的数字个数,然后是数字本身。最后,程序输出相邻奇数对的数量。这是示例输出:
输入序列长度:6
输入数字 1: 3
输入数字 2: 4
输入数字 3: 7
输入数字 4: 9
输入数字 5: 3
输入数字 6: 5
相邻奇数的对数为3
我想出了以下代码:
length = eval(input("Enter the length of the sequence: "))
for i in range(1,length+1):
ask = eval(input("Enter number: "+str(i)+ ": "))
for m in range(0,length+1,2):
ask2 = ask
h = ask%2
f = ask2%2
if h>0 and f>0:
k = (len(str(ask) + str(ask2)))
print(k)
else:
pass
虽然提示的输出是正确的,但我无法计算相邻奇数对的数量。请帮助我更正我的代码或在此基础上进行构建;这将不胜感激。您一定已经注意到,我一直在使用基本的 if 语句、循环和字符串来编写代码。如果你能坚持这个让我更好地理解,那就太好了。
抱歉这么久 post。
非常感谢
检查当前元素和下一个元素是否都是奇数和和:
length = int(input("Enter the length of the sequence: "))
nums = [int(input("Enter number: {}: ".format(i))) for i in range(1, length + 1)]
print(sum(ele % 2 and nums[i] % 2 for i,ele in enumerate(nums, 1)))
enumerate(nums, 1)
从 1
开始索引,因此 ele % 2 and nums[i] % 2
在我们用下一个相邻数字遍历 nums 时检查当前元素。
当你想转换为 int 时使用 int(input..
,使用 eval 永远不是一个好主意。您还应该使用 while
循环并使用 try/except
.
不使用列表:
length = int(input("Enter the length of the sequence: "))
total = 0
# get a starting number
ask = int(input("Enter number: {}".format(1)))
# will keep track of previous number after first iteration
prev = ask
for i in range(2, length + 1):
ask = int(input("Enter number: {}".format(i)))
# if current and previous are both odd increase count
if ask % 2 and prev % 2:
total += 1
# update prev
prev = ask
print(total)
这是一种使用简单 for
循环的方法。就像 Padraic 的解决方案一样,它使用列表来累积输入,因为它比在读入时尝试测试对更简单。正如 Padraic 提到的,你真的应该检查输入是否正确。
length = int(input("Enter the length of the sequence: "))
seq = []
for i in range(1, length + 1):
ask = int(input("Enter number " + str(i) + ": "))
seq.append(ask)
count = 0
for i in range(length - 1):
if seq[i] % 2 == 1 and seq[i+1] % 2 == 1:
count += 1
print("The number of pairs of adjacent odd numbers is", count)
这将解决您的问题。
首先将用户给出的输入放入名为numList的列表中。保留一个计数变量来统计相邻奇数的个数。一个一个循环遍历numList,通过检查除以2的余数来识别奇数。(通过给定的if条件检查)然后您可以简单地打印列表中相邻奇数的数量。
length=int(input("Enter the length of the sequence: "))
numList=[]
count=0
for i in range(length):
num=int(input("Enter number "+str(i+1)+ ": "))
numList.append(num)
for x in range(len(numList)-1):
num1=numList[x]
num2=numList[x+1]
if((num1%2==1) and (num2%2==1)):
count=count+1
else:
continue
print("The number of pairs of adjacent odd numbers is "+str(count))
如果您想在不使用列表的情况下解决这个问题,这就是答案。 您应该在接收输入时处理输入。
length=int(input("Enter the length of the sequence: "))
count=0
num1=int(input("Enter number "+str(1)+ ": "))
for i in range(length-1):
num2=int(input("Enter number "+str(i+2)+ ": "))
if((num1%2==1) and (num2%2==1)):
count=count+1
num1=num2
print("The number of pairs of adjacent odd numbers is "+str(count))