Python: Code is ignoring the if and else
kevin4fong at gmail.com
kevin4fong at gmail.com
Fri Aug 2 22:24:56 EDT 2013
On Friday, August 2, 2013 7:11:37 PM UTC-7, Joshua Landau wrote:
> On 3 August 2013 02:44, <kevin... at gmail.com> wrote:
>
>
>
> Yeah, I already know about that. But if I try to change it, I'm not even able to start the program. If I try to change the if statement that it corresponds with, I get a an error saying "card" is not a global. And if I try to shift it in, for some reason...the program runs through the MISS line multiple times.
>
>
>
>
>
> You have a car with a broken engine and a broken tire and are telling us that you refuse to fix the engine because it highlights the problem of the broken tire.
>
>
> Take the fix and move on to the next problem.
>
>
>
>
> One piece of advice is about scoping. This is perhaps the hardest "gotcha" of Python conceptually, but it's sensible once you understand the justifications.
>
>
> Run the four commands below and try and understand why this applies to your code.
>
>
>
>
> a = 1
>
>
> def access_global():
> print(a)
>
>
> def set_variable():
> a = 2
> print(a)
>
>
> def broken_set():
>
>
> a = a + 1
> print(a)
>
>
> def also_broken():
> print(a)
> return
> a = 1 # Never run!
>
>
> The fix for the broken variants is to start the functions with "global a".
I'll take a look at those. I used that fix you brought up as well but the main issue I get after that is:
I'll try to explain the main issue I'm having as accurately as possible. Basically, I've changed the indentations around but the main issue I get is that the GetTopCard(sDeck) line is being run multiple times for some reason. So when I run the program, I get "MISS: You fished up the rank......" multiple times and my list pHands[0] is increased by several number combinations when I only want it to increase by one. I also updated that last definition in my main code.
def player_0_hitman(hit):
for card in pHands[target_player]:
if target_card[0] == card[0]:
count = pHands[target_player].count(card)
pHands[0].append(card)
pHands[target_player].remove(card)
ShowMessage("HIT: " + str(count) + " card(s) transferred")
else:
top_card = GetTopCard(sDeck)
pHands[0].append(top_card)
if top_card[0] == target_card[0]:
ShowMessage("HIT: LUCKILY Player 0 has fished up a rank <" + str(top_card[0]) + ">!!!")
else:
ShowMessage("MISS: You fished up the rank <" + str(top_card[0]) + ">")
hit = hit - 1
More information about the Python-list
mailing list