Python运行一个函数,我没有调用它 [英] Python runs a function without me calling it

查看:69
本文介绍了Python运行一个函数,我没有调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在python中制作了一个简单的二十一点游戏,请看这里:

if have made a simple blackjack game in python see here:

import random
deckOfCards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
playerHand = []
computerHand = []

def testWin():
    if sum(playerHand) == sum(computerHand):
        print("Draw")
    elif sum(playerHand) == 21:
        print("Blackjack! You win")
    elif sum(computerHand) == 21:
        print("Computer has blackjack you lose")

    if sum(playerHand) > 21:
        if sum(computerHand) < 21:
            print("You lost")
        elif sum(computerHand) > 21:
            print("Draw")
    elif sum(computerHand) > 21:
        if sum(playerHand) < 21:
            print("You win")
        elif sum(playerHand) > 21:
            print("Draw")
    elif sum(playerHand) < 21:
        if sum(computerHand) > 21:
            print("You win!")
        elif sum(computerHand) < 21 and sum(computerHand) < sum(playerHand):
            print("You win")
        elif sum(computerHand) < 21 and sum(computerHand) > sum(computerHand):
            print("You lose")


def drawPlayerCard():
        playerHand.append(deckOfCards[random.randint(0, 9)])
        print("Your Cards are:", playerHand)
        print("total:", sum(playerHand), "\n")
        if len(playerHand) < 2:
            drawPlayerCard()
        drawComputerHand()


def drawComputerHand():
    if sum(computerHand) <= 17:
        computerHand.append(deckOfCards[random.randint(0, 9)])
        print("the computer has:", computerHand)
        print("total:", sum(computerHand), "\n")
        if len(computerHand) < 2:
            drawComputerHand()
        hit_stand()
    else:
        print("the computer stands with a total of:", sum(computerHand))
        hit_stand()


def hit_stand():
        option = input("do you want to hit or stand? [h/s]")
        if option.lower() == "h":
            drawPlayerCard()
        elif option.lower() == "s":
            testWin()
        else:
            print("please say if you want to hit or stand!")
            hit_stand()


def start():
    start_gaming = input("Do you want to play Blackjack? [y/n]")
    if start_gaming == "y":
        drawPlayerCard()
    elif start_gaming == "n":
        pass
    else:
        print("please state if you want to start the game")
        start()


start()



,一切都按预期工作,直到我站起来,而不是表现得非常奇怪。我不知道我怎么解释它所以它只会显示它:


and everything works as intended until i do stand than it behaves really weird. i dont know how i could explain it so it will just show it:

引用:

你想玩吗?酒杯? [y / n] y

你的卡片是:[9]

总计:9



你的卡片是:[9,6]

总计:15



电脑有:[8]

总计:8



电脑有:[8,10]

总计:18



你想打或站吗? [h / s] h

你的卡片是:[9,6,9]

总计:24



计算机支架总计:18

你想打或站吗? [h / s] s

你输了

你想打或站吗? [h / s] s

你输了

计算机支架总计:18

你想打或站吗? [h / s] s

你丢了

Do you want to play Blackjack? [y/n]y
Your Cards are: [9]
total: 9

Your Cards are: [9, 6]
total: 15

the computer has: [8]
total: 8

the computer has: [8, 10]
total: 18

do you want to hit or stand? [h/s]h
Your Cards are: [9, 6, 9]
total: 24

the computer stands with a total of: 18
do you want to hit or stand? [h/s]s
You lost
do you want to hit or stand? [h/s]s
You lost
the computer stands with a total of: 18
do you want to hit or stand? [h/s]s
You lost



似乎python正在调用drawComputerHand函数和hit_stand函数3次,但我不知道它来自哪里以及如何解决它。

所以,如果有人看到问题,请你解释一下并建议一种解决方法

对不起那些糟糕而低效的代码感到抱歉,但我是初学者,还在努力学习


it seems like the python is calling the drawComputerHand function and the hit_stand function 3 times but i dont see where its coming from and how to fix it.
so if someone sees the problem could you please explain it and suggest a way to fix it
sorry for the maybe bad and inefficient code but im a beginner and still trying to learn

<pre lang="Python"><pre lang="Python"><pre lang="Python">





我的尝试:



i看了在我的代码2小时,我搜索谷歌上的人有同样的问题和修复,我已经发布了stackoverflow上的问题,但我无法得到答案,现在我尝试h ere



What I have tried:

i have looked at my code for 2 hours, i have searched on google wheter people had the same problem and a fix, i have posted the problem on stackoverflow but i couldnt get a answer and now im trying here

推荐答案

引用:

我看了两个小时的代码



看你的代码表现怎么样?

你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么和你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到bug,它只是帮助你通过向您展示正在发生的事情。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]

调试器只向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。


What about watching your code performing ?
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


看来你在那里有一个递归循环。你打电话给 hit_stand ,它调用 drawPlayerCard ,它调用 drawComputerHand ,然后再次调用 hit_stand 。我认为你的逻辑需要一点工作。
It appears you have a recursive loop in there. You call hit_stand, which calls drawPlayerCard, which calls drawComputerHand, which then calls hit_stand again. I think your logic needs a little work.


这篇关于Python运行一个函数,我没有调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆