[英]Why does my basic print statement work, but my f-string version produces a syntax error?
Very early stages of learning Python, and trying to complete 100 days of code challenge.非常早期的学习 Python,并尝试完成 100 天的代码挑战。 Am building a crude blackjack game, but am stuck on what I am sure is probably a very simple fix that I don't recognise!
我正在构建一个粗糙的二十一点游戏,但我确信这可能是一个我不认识的非常简单的修复!
This is my code so far:到目前为止,这是我的代码:
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
players = ["player", "dealer"]
players_hands = {}
players_scores = {}
for player in players:
cards = random.choices(cards, k = 2)
players_hands[player] = cards
players_scores[player] = sum(cards)
print(players_hands["player"])
print(players_scores["player"])
print(f"Your cards: {players_hands["player"]}, current score: {players_scores["player"]}")
print(f"Dealer's first card: {players_hands["dealer"][0]}")**
I want to print the f-string version rather than the basic print statement, but keep getting a syntax error and don't know what I've done wrong.我想打印 f-string 版本而不是基本的打印语句,但不断收到语法错误并且不知道我做错了什么。
You should check that you are using at least Python 3.6, which f-strings were introduced in, but also...您应该检查您是否至少使用了 Python 3.6,其中引入了 f 字符串,但也...
You are using double quotes to access the dictionary, which is conflicting with using double quotes for the string.您正在使用双引号来访问字典,这与对字符串使用双引号相冲突。 Try changing to either
尝试更改为
print(f"Dealer's first card: {players_hands['dealer'][0]}")
or或者
print(f'Dealer\'s first card: {players_hands["dealer"][0]}')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.