Python 2.7.10在两台不同的计算机上以不同的方式运行代码

时间:2015-08-25 17:04:05

标签: python windows

我的程序在我的桌面上运行,但在我的笔记本电脑上运行,即使它安装了相同版本的Python。

  • 桌面运行Windows 7和
  • 笔记本电脑运行Windows 8.1,但我升级到Windows 10,看看是否解决了这个问题。它没。

有人可以告诉我我做错了吗?

import random

count = 0
food_list = [
    "Pizza", 
    "Italian", 
    "Hamburger", 
    "Sandwiches", 
    "Salads",
    "Chinese",
    "Seafood",
    "Mexican",
    "French",
    "Deli",
    "Indian"
]
food_choices = []

# Header and description text
print " "
print "*" * 30
print "*" * 30
print " "
print "Food Options 1000"
print " "
print "Tell the Food Options 1000 how many options you want to select from"
print "and it will provide you with that amount of appropriate food choices."
print " "
print "*" * 30
print "*" * 30
print " "
# --- End header and description text ---

print "You have a total of %d choices from which to choose." % len(food_list)
print " "

# Receive user input and assign to a variable
optNum = int(raw_input("How many options do you want to have? "))

# Show how many choices user selected
print " "
print "Your %d choices are: " % optNum
print " "

# Run while loop until user selection integer is met
# Print food choices in the amount of user chosen number
while count != optNum:
    fc = random.choice(food_list)
    if fc not in food_choices:
        food_choices.append(fc)
        count += 1

for i in food_choices:
    print i

print " "
print " "

我的桌面结果:

  

您想要多少个选项? 8

     

您的8个选择是:

     

Salads Deli墨西哥中式披萨汉堡法式三明治

笔记本电脑的结果:

  

您想要多少个选项? 8

     

您的8个选择是:

     

Salads Deli墨西哥中国比萨墨西哥法国法国

更新:

我按照f.rodrigues的建议,通过USB记忆棒将文件移到笔记本电脑上。棒上的文件正常工作,不显示重复。复制粘贴代码中有47个缺失的行。

谢谢大家。

2 个答案:

答案 0 :(得分:1)

我发现很难相信你正在使用相同的代码。

你说你把它复制粘贴到另一台机器上。

如果是这种情况在这个过程中可能出现问题,那么您的IDE可能会弄乱缩进。 (可能是由于错过使用制表符和空格作为缩进)

这样的事情可能会发生:

while count != optNum:
    fc = random.choice(food_list)
    if fc not in food_choices:
        food_choices.append(fc)
    count += 1

与此完全不同:

while count != optNum:
    fc = random.choice(food_list)
    if fc not in food_choices:
        food_choices.append(fc)
        count += 1

缺少简单的缩进可能会产生不同的结果。在这种情况下,它会向food_choice添加更少的项目。

编辑:

msw表示在OP问题中,两个列表都有8个项目,只有第二个列表有重复项。

为此,我认为差异来自于此:

while count != optNum:
    fc = random.choice(food_list)
    food_choices.append(fc)
    count += 1

检查唯一性的行不存在。

这将获得列表中相同数量的项目,但它可能有重复项。

这有点远离我开始的复制粘贴问题。

我认为可以完全确定的唯一方法是使用相同的文件,而不是复制它。使用USB棒,或在线上传/下载。

答案 1 :(得分:0)

出现第一个问题是因为您使用的是随机数字生成器。伪随机算法以种子值开始。如果你没有明确地播种它,它将使用一天中的时间(或其他任意数字)播种。因此,每次运行程序时,random生成的数字都会有所不同。这允许您将随机生成器设置为相同的起始种子,因此所有运行将给出相同的结果。如果您在程序开头使用random.seed('myseed')甚至random.seed(4),那么每次运行都是相同的。

您的代码的第二个问题是您使用random.choice()来选择您的选择;你将以这种方式获得重复,例如"法语法语"在您的示例输出中。最好使用random.sample(food_list, optNum),其中sample被描述为

  

返回从总体序列中选择的k长度的唯一元素列表。用于无需替换的随机抽样。

     

返回包含来自总体的元素的新列表,同时保持原始总体不变。 ...

相关问题