Python 输入提示赢得';t端
运行Python 3.6.5 我对python非常陌生。当我在终端中分别运行这些线路时,我得到的正是我想要的。当我运行python文件时,输入提示“理想重量”不会在我提交数字后结束。它不断重复“理想体重”。我试图从“权重”集合中找到数字的组合,这些组合将汇总到用户输入Python 输入提示赢得';t端,python,python-3.x,Python,Python 3.x,运行Python 3.6.5 我对python非常陌生。当我在终端中分别运行这些线路时,我得到的正是我想要的。当我运行python文件时,输入提示“理想重量”不会在我提交数字后结束。它不断重复“理想体重”。我试图从“权重”集合中找到数字的组合,这些组合将汇总到用户输入 import itertools weights = [3, 3, 7.5, 7.5, 10] weightint = int(input('ideal weight? ')) result = [seq for i in ran
import itertools
weights = [3, 3, 7.5, 7.5, 10]
weightint = int(input('ideal weight? '))
result = [seq for i in range(len(weights), 0, -1) for seq in itertools.combinations(weights, i) if sum(seq) == weightint]
print(result)
有人能解释一下我做错了什么吗。谢谢大家! 不确定你的终端出了什么问题。考虑使用而不是<代码>输入<代码>:
import itertools
import argparse
MY_WEIGHTS = [3, 3, 7.5, 7.5, 10]
def find_weight(w):
result = [seq for i in range(len(MY_WEIGHTS), 0, -1) for seq in itertools.combinations(MY_WEIGHTS, i) if sum(seq) == w]
return result
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-w', '--weight', required=True, type=int, help='The weight')
args = parser.parse_args()
result = find_weight(args.weight)
print('result: {}'.format(result))
if __name__ == '__main__':
main()
然后使用--weight
或-w
从命令行调用它:
python3 ./weight.py --weight 10
result: [(10,)]
你的代码似乎对我有用。。。也许可以尝试其他终端来执行代码。python文件名是什么?@GhasemNaddaf weight.py以及如何运行它?有关此代码的任何内容都不会重复输入提示。你确定这就是你正在运行的代码吗?