我是一个在Spyder上使用Python 3.7的初学者程序员,当我运行我的代码时,它会打开一个system32命令提示符,然后立即关闭。什么也没发生。当我尝试按下调试按钮时,也会发生同样的事情。我将在此处粘贴我的代码。 编辑:感谢oliverm,我已经更新了代码,但代码仍然无法启动。
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
#imports
import random
#def the vars
credits = 10
playagain = None
jack = 10
queen = 10
king = 10
ace = 11 #Nice
x = 0
y = 0
a = 0
b = 5
playing = True
cardamount = 0
deck = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,jack,jack,jack,jack,queen,queen,queen,queen,king,king,king,king,ace,ace,ace,ace]
#def the functions
def greetings():
print("Greetings! Weclome to Blackjack! dDo you wish to play?")
print ("Yes or No")
answer = input()
if answer == "Yes" or "yes" or "y" or "Y":
blackjack()
elif answer == "No" or "no" or "n" or "N":
print ("Alright then. Have a good day!")
else:
print("I have no idea what you just said. can you try that again?")
return
def bets():
print ("How much will you bet?")
print ("You have ",credits, " credits")
bet = int(input())
print ("Alright! Lets start!")
pass
def playerplay():
card1 = random.choice(deck)
card2 = random.choice(deck)
#ace logic here. an if statement.
cardlist = [card1, card2]
if card1 == ace and card2 == ace:
pastace = 2
card1 = 1
card2 =11
elif card1 == ace:
pastace = 1
if card2 + 11 > 21:
ace = 1
else:
ace = 11
elif card2 == ace:
pastace = 1
if card1 + 11 > 21:
ace = 1
else:
ace = 11
else:
pastace = 0
pass
cardamount = card1 + card2
print ("You drew a ", card1, " and a ", card2)
while playing == True:
print ("You currently have ", cardamount, " points with ", pastace, "aces" )
print ("Hit or Stand?")
hvalue = str(input())
if hvalue == 'Hit' or "hit" or "H" or "h":
cardlist.append(random.choice(deck))
#calculates cardamount
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
#paste ace logic here
if cardlist[x + 1] == ace:
pastace += 1
if cardamount > 21 and pastace == 1:
ace = 1
cardamount == card1 + card2
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
if cardamount > 21:
print("You have drawn over 21 cards. Now the dealer will play.")
playing = False
del cardlist[0:x]
pcardamount = cardamount
pastace = 0
x = 0
pass
else:
x += 1
return
elif cardamount > 21 and pastace > 1:
ace = 1
cardamount = card1 + card2
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
if cardamount > 21:
print("You have drawn over 21 cards. Now the dealer will play.")
playing = False
del cardlist[0:x]
pastace = 0
pcardamount = cardamount
x = 0
pass
elif cardamount - 22 > -11:
ace in cardlist = 11
return
else:
x += 1
return
else:
x += 1
return
elif hvalue == 'Stand' or 'stand' or 'S' or "s":
print ('Alright! Your total card amount is ', cardamount)
playing = False
del cardlist[0:x]
pastace = 0
pcardamount = cardamount
x = 0
pass
else:
print ("I'm sorry. What did you say?")
return
def outcome():
print("Your total card amount was", pcardamount, "and the dealer's was ", dcardamount)
if pcardamount > dcardamount and pcardamount <= 21 :
print("You win!")
credits += bet*2
elif pcardamount < dcardamount and dcardamount <=21 :
print("You lose...")
credits -= bet
else:
print("It's a tie!")
#says who wins here and defines playagain
playing = True
print("You now have ", credits, " credits.")
print ("Do you wish to play again?")
answer2 = str(input())
if answer2 == "Yes" or "yes" or "y" or "Y":
blackjack()
elif answer2 == "No" or "no" or "n" or "N":
print("Thank you for playing! Have a great day!")
else:
print: ("I have no idea what you just said. can you try that again?")
return
def hit():
cardlist.append(random.choice(deck))
#calculates cardamount
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
#paste ace logic here
if cardlist[x + 1] == ace:
pastace += 1
if cardamount > 21 and pastace == 1:
ace = 1
cardamount == card1 + card2
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
if cardamount > 21:
del cardlist[0:x]
pastace = 0
x = 0
dcardamount = cardamount
cardamount = 0
pass
else:
x += 1
return
elif cardamount > 21 and pastace > 1:
ace = 1
cardamount = card1 + card2
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
if cardamount > 21:
playing = False
del cardlist[0:x]
pastace = 0
x = 0
dcardamount = cardamount
cardamount = 0
pass
elif cardamount - 22 > -11:
ace in cardlist = 11
return
else:
x += 1
return
else:
x += 1
def dealerplay():
card1 = random.choice(deck)
card2 = random.choice(deck)
#ace logic here. an if statement.
cardlist = [card1, card2]
if card1 == ace and card2 == ace:
pastace = 2
card1 == 1
card2 ==11
elif card1 == ace:
pastace = 1
if card2 + 11 > 21:
ace = 1
else:
ace = 11
elif card2 == ace:
pastace = 1
if card1 + 11 > 21:
ace = 1
else:
ace = 11
else:
pastace = 0
pass
cardamount = card1 + card2
if cardamount < 17:
hit()
return
else:
del cardlist[0:x]
pastace = 0
x = 0
dcardamount = cardamount
cardamount = 0
pass
def blackjack():
bets()
playerplay()
dealerplay()
outcome()
#game functions put together
greetings()
关于:
"缩进中制表符的使用不一致。">
您需要确保使用制表符或简单空格作为空格字符。混合两者将导致上述错误。您可以将您的 spyder 配置为在按下 tab 键时使用例如 4 个意图。
根据Spyder Python缩进的答案 尝试
在 Spyder v3.0.0 中,转到源 -->修复缩进。这对我有用。
在开始调试之前,需要修复语法错误。
若要使用调试器,需要在代码中或在启动调试器时定义断点。Spyder 在 ipython 控制台中使用 ipdb 调试器。看看关于调试的 spyder 文档: https://docs.spyder-ide.org/debugging.html
从他们的文档中,您可以使用以下选项来设置断点:
为在编辑器中打开的文件中的任何行设置和清除正常和条件断点的多种方法。
- 通过从"调试"菜单中选择相应的选项。
- 通过按可配置的键盘快捷键(默认情况下为 F12 表示正常,Shift-F12 表示条件断点(。
- 通过双击打开文件中行号的左侧。
- 在代码中使用 ipdb.set_trace(( 语句(导入 pdb 后(。
- 以交互方式,在 ipdb 会话中使用 b 命令。
查看例如:https://realpython.com/python-debugging-pdb/以获取快速调试教程。
我已经在下面的代码中快速修复了您的语法问题。此外,如果传递了输入,则用于选择播放或不播放的 if 子句将更改为正确计算为 True 或 False。
现在应该可以进行调试。为了帮助您入门,我在脚本的开头导入了 ipdb,并在问候函数(第 29 行(中设置了一个断点。
关于窗口关闭的问题,请检查在"工具"->"首选项"->"运行"-">控制台"中设置的内容。如果您希望程序在Spyder的当前iPython控制台中执行,请选择"在当前控制台中执行"。
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
#imports
import random
import ipdb
#def the vars
credits = 10
playagain = None
jack = 10
queen = 10
king = 10
ace = 11 #Nice
x = 0
y = 0
a = 0
b = 5
playing = True
cardamount = 0
deck = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,jack,jack,jack,jack,queen,queen,queen,queen,king,king,king,king,ace,ace,ace,ace]
# def the functions
def greetings():
ipdb.set_trace()
print("Greetings! Weclome to Blackjack! n Do you wish to play?")
print("Yes or No")
answer = input()
if answer in ["Yes", "yes", "y", "Y"]:
blackjack()
elif answer in ["No", "no", "n", "N"]:
print("Alright then. Have a good day!")
return
else:
print("I have no idea what you just said. can you try that again?")
return
def bets():
print("How much will you bet?")
print("You have ", credits, " credits")
bet = int(input())
print("Alright! Lets start!")
return bet
def playerplay():
card1 = random.choice(deck)
card2 = random.choice(deck)
#ace logic here. an if statement.
cardlist = [card1, card2]
if card1 == ace and card2 == ace:
pastace = 2
card1 = 1
card2 =11
elif card1 == ace:
pastace = 1
if card2 + 11 > 21:
ace = 1
else:
ace = 11
elif card2 == ace:
pastace = 1
if card1 + 11 > 21:
ace = 1
else:
ace = 11
else:
pastace = 0
pass
cardamount = card1 + card2
print ("You drew a ", card1, " and a ", card2)
while playing == True:
print ("You currently have ", cardamount, " points with ", pastace, "aces" )
print ("Hit or Stand?")
hvalue = str(input())
if hvalue == 'Hit' or "hit" or "H" or "h":
cardlist.append(random.choice(deck))
#calculates cardamount
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
#paste ace logic here
if cardlist[x + 1] == ace:
pastace += 1
if cardamount > 21 and pastace == 1:
ace = 1
cardamount == card1 + card2
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
if cardamount > 21:
print("You have drawn over 21 cards. Now the dealer will play.")
playing = False
del cardlist[0:x]
pcardamount = cardamount
pastace = 0
x = 0
pass
else:
x += 1
return
elif cardamount > 21 and pastace > 1:
ace = 1
cardamount = card1 + card2
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
if cardamount > 21:
print("You have drawn over 21 cards. Now the dealer will play.")
playing = False
del cardlist[0:x]
pastace = 0
pcardamount = cardamount
x = 0
pass
elif cardamount - 22 > -11:
ace in cardlist == 11
return
else:
x += 1
return
else:
x += 1
return
elif hvalue == 'Stand' or 'stand' or 'S' or "s":
print ('Alright! Your total card amount is ', cardamount)
playing = False
del cardlist[0:x]
pastace = 0
pcardamount = cardamount
x = 0
pass
else:
print ("I'm sorry. What did you say?")
return
def outcome():
print("Your total card amount was", pcardamount, "and the dealer's was ", dcardamount)
if pcardamount > dcardamount and pcardamount <= 21 :
print("You win!")
credits += bet*2
elif pcardamount < dcardamount and dcardamount <=21 :
print("You lose...")
credits -= bet
else:
print("It's a tie!")
#says who wins here and defines playagain
playing = True
print("You now have ", credits, " credits.")
print ("Do you wish to play again?")
answer2 = str(input())
if answer2 in ["Yes", "yes", "y", "Y"]:
blackjack()
elif answer2 in ["No", "no", "n", "N"]:
print("Alright then. Have a good day!")
return
else:
print("I have no idea what you just said. can you try that again?")
return
def hit():
cardlist.append(random.choice(deck))
#calculates cardamount
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
#paste ace logic here
if cardlist[x + 1] == ace:
pastace += 1
if cardamount > 21 and pastace == 1:
ace = 1
cardamount == card1 + card2
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
if cardamount > 21:
del cardlist[0:x]
pastace = 0
x = 0
dcardamount = cardamount
cardamount = 0
pass
else:
x += 1
return
elif cardamount > 21 and pastace > 1:
ace = 1
cardamount = card1 + card2
for y in len(cardlist):
cardamount += cardlist[y]
if y == len(cardlist):
y = 0
break
else:
y += 1
if cardamount > 21:
playing = False
del cardlist[0:x]
pastace = 0
x = 0
dcardamount = cardamount
cardamount = 0
pass
elif cardamount - 22 > -11:
ace in cardlist == 11
return
else:
x += 1
return
else:
x += 1
def dealerplay():
card1 = random.choice(deck)
card2 = random.choice(deck)
#ace logic here. an if statement.
cardlist = [card1, card2]
if card1 == ace and card2 == ace:
pastace = 2
card1 == 1
card2 ==11
elif card1 == ace:
pastace = 1
if card2 + 11 > 21:
ace = 1
else:
ace = 11
elif card2 == ace:
pastace = 1
if card1 + 11 > 21:
ace = 1
else:
ace = 11
else:
pastace = 0
pass
cardamount = card1 + card2
if cardamount < 17:
hit()
return
else:
del cardlist[0:x]
pastace = 0
x = 0
dcardamount = cardamount
cardamount = 0
pass
def blackjack():
bets()
playerplay()
dealerplay()
outcome()
#game functions put together
greetings()