【问题标题】:Python-Returning to a specific point in the codePython-返回到代码中的特定点
【发布时间】:2015-11-27 04:59:06
【问题描述】:

所以我正在编写一些代码作为一个有趣的项目,它将随机生成一个主题供我每天学习。但是一旦一个主题出现了一次,我不希望它出现在本周剩下的时间里。为此,我使用了一个列表。基本上,当它选择主题时,它会将其添加到列表中,并且下次它检查它是否已经在列表中,如果是,我希望它返回到随机数生成器。我该怎么做呢?这是我的代码。

import random
import time
import datetime

#Subject veto list
x=[]

# Messages to instil, um, enthusiasm.
if datetime.date.today().strftime("%A") == "Monday":
    response = input("Here we go again. Are you ready? ")
elif datetime.date.today().strftime("%A") == "Tuesday" or "Wednesday" or "Thursday":
    response = input("Are you ready? ")
elif datetime.date.today().strftime("%A") == "Friday":
    response = input("Half day! Are you ready? ")
elif datetime.date.today().strftime("%A") == "Saturday" or "Sunday":
    response = input("It's the weekend! Are you ready? ")

# Random picking of subject to study. Also adds sbject to veto list for rest of week.
if response == "Yes":
        subject = random.randint(1, 7)
        print("Today you are studying...")
        time.sleep(3)
        if subject == (1):
            "Englsh" in x
            print("English")
            x.extend([English])
        elif subject == (2):
            "Irish" in x
            print("Irish")
            x.extend([Irish])
        elif subject == (3):
            "Maths" in x
            print("Maths")
            x.extend([Maths])
        elif subject == (4):
            "French" in x
            print("French")
            x.extend([French])
        elif subject == (5):
            "Physics" in x
            print("Physics")
            x.extend([Physics])
        elif subject == (6):
            "Chemistry" in x
            print("Chemistry")
            x.extend([Chemistry])
        elif subject == (7):
            "History" in x
            print("History")
            x.extend([History])

【问题讨论】:

  • 问题是否决列表不会持续存在。这意味着每次运行程序时,列表都是空的,因此所有选项都将始终可用。如果您想在一段时间内阻止相同的选项,则需要通过将有关您最近选择的选项的信息存储到数据库或文件中来持久保存它。
  • 如果我让程序持续运行会怎样?
  • 只要您的计算机没有重新启动等,它就可以工作。确实,尽管您希望保留这些信息。
  • 正如@polpak 所说,您需要将该列表存储到某种文件或某个“数据库”中。所以程序从那里读取。而不是比较哪个主题已经被随机挑选。读取和写入您可以在此处找到的文件:stackoverflow.com/questions/6159900/…
  • 为什么你有,例如,"History" in x?您正在浪费周期进行比较而不是存储结果。此外,"something" == "x" or "y" 并没有按照您的想法行事。

标签: python list loops return


【解决方案1】:

此功能可让您根据工作日从列表中选择随机事物,无需重复*,也无需在运行之间存储任何内容。唯一的潜在问题是 Python 的 PRNG 是否在周中更改:P。

import datetime
import itertools
import random

def random_choice_per_date(iterable, date=None):
    choices = list(itertools.islice(iterable, 7))
    date = date or datetime.date.today()

    year, week, weekday = date.isocalendar()

    rand = random.Random((year, week))  # Seed PRNG with (year, week)
    rand.shuffle(choices)

    index = weekday % len(choices)

    return choices[index]

这可以在日期之外推广,但我不想使代码复杂化。

* 如果可迭代对象少于七个项目,它重复。它也最多只使用前七项。

【讨论】:

    【解决方案2】:

    希望下面的 sn-p 对你有所帮助。
    它向您展示了如何从列表中获取随机元素。
    获取该元素后,列表从列表中取出该元素。

    import random
    
    def get_random_and_pop(lst):
        value = random.choice(lst)
        lst = list(set(lst) - set([value]))
        return value,lst
    
    lst = ['english','math','science','computer science','history']
    while (len(lst)):
        v,lst = get_random_and_pop(lst)
        print v,lst
        if (len(lst) == 0):
            lst = ['english','math','science','computer science','history']
    

    【讨论】:

    • 谢谢,请问有办法每周重置吗?即当没有元素时?
    • @pzp 我不是来为 OP 解决这个问题的。这是另一个问题。
    • 当我运行代码时,在 "print v,lst" @taesu 中的 v 下出现语法错误
    • 试试print(v,lst),我猜你用的是py3
    【解决方案3】:

    如果你将选择算法放在一个函数中,并迭代该函数直到返回的值没有出现在“禁止”列表中,然后打印结果的非禁止值,你就完成了!

    实际上不要这样做,这将是一种糟糕的方法。当它被列入禁止列表时,只需将其从选项列表中删除..然后在一周后将其移回。

    【讨论】:

    • 我该怎么做呢?对不起我的无能:(
    • 我认为有几个答案可以说明如何,请查看@taesu 的答案
    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多