python-如果已经给出了提示,请转到尚未使用的其他随机提示

时间:2018-08-29 10:40:30

标签: python

我在制作这个游戏时会猜出数字,并提示您该数字是多少。我是为提示系统制作的。

Fatal error: Uncaught Error: Call to a member function connection() on null in C:\inetpub\wwwroot\blog2\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1138 Stack trace: #0 C:\inetpub\wwwroot\blog2\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1104): Illuminate\Database\Eloquent\Model::resolveConnection(NULL) #1 C:\inetpub\wwwroot\blog2\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(936): Illuminate\Database\Eloquent\Model->getConnection() #2 C:\inetpub\wwwroot\blog2\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(847): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder() #3 C:\inetpub\wwwroot\blog2\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(885): Illuminate\Database\Eloquent\Model->newModelQuery() #4 C:\inetpub\wwwroot\blog2\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(836): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes() #5 C:\inetpub\wwwroot\blog2\vendor\laravel\framework\s in C:\inetpub\wwwroot\blog2\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 1138

如果使用if str(userinp) == ('hint'): g = randint(1,3) if g == 1 and g1 == True: print('The number',(hint_1),'than 50') g1 = False elif g == 2 and g2 == True: print('The number',(hint_2)) g2 = False elif g == 3 and g3 == True: print('the number',(hint_3)) elif g == 4 and g4 == True: print('WIP') 选择了重复的提示,我该怎么做,它将转到另一个未使用的提示?

谢谢:)。

2 个答案:

答案 0 :(得分:6)

您的错误在于使用单独的变量作为提示。而是将它们放在列表中,以便您可以使用random.shuffle()

# at the start
hints = [
    "The number {} than 50".format(hint_1)
    "The number {}".format(hint_2)
    "The number {}".format(hint_3)
]
random.shuffle(hints)

# in the game loop
if userinp == 'hint':
    if not hints:
        print("Sorry, you are out of hints")
    else:
        hint = hints.pop()  # take **one** of the randomised hints
        print(hint)

random.shuffle()以随机顺序放置hints列表,重复的hints.pop()调用将以随机顺序选择下一个可用提示。

请注意,现在hints列表为空时,用户也不再需要保留单独的提示标志。

请注意:在布尔测试中使用== True是没有意义的。 if已经测试过布尔真实性,添加== True是多余的(当您必须测试布尔对象时,仅将is True用作{{1 }}和True是单例)。

答案 1 :(得分:2)

您可以将提示放在列表中,然后在列表中选择一个随机条目,然后将其从列表中删除。

hints = ["Something warm", "Fuzzy", "Meows"]
if str(userinput) == "hint":
    hintn = randint(0,len(hints)-1) # assuming the range of randint is inclusive
    print(hints[hintn])
    del hints[hintn]

或者像马丁·马丁(Martijn)在评论中所建议的那样,将其随机播放一次,然后从中弹出。

from random import shuffle
hints = ["Something warm", "Fuzzy", "Meows"]
shuffle(hints)
#...
if str(userinput) == "hint":
    if not hints:
        print("No hints remaining")
    else:
        print(hints.pop())