简体   繁体   English

为什么我的基本打印语句有效,但我的 f-string 版本会产生语法错误?

[英]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.

相关问题 为什么此打印语句使用 Python f-string output 双括号? - Why does this print statement using a Python f-string output double parentheses? 为什么在 f 字符串中索引匿名元组会出错? - Why does indexing anonymous tuple in f-string give an error? 带有嵌套引号的 f 字符串的语法错误 - Syntax error on an f-string with nested quotation 为什么 f-string 文字在这里不起作用,而 %()s 格式可以? - Why does f-string literal not work here, whereas %()s formatting does? 为什么 print 和 f-string 在不同的时间执行评估? - Why do print and f-string perform evaluation at different times? 为什么我的打印语句(python)在我的 python 3.4.3 shell 中工作,而不是在线编译器? 是因为版本不同吗? - Why does my print statement(python) work in my python 3.4.3 shell, but not the online compiler? Is it because it's a different version? RuntimeError:操作在 f 字符串语句中没有标识 - RuntimeError: Operation does not have identity in f-string statement 为什么我不能在我的 f-string 中输入一个浮点数作为 integer - Why can't i type a float as an integer in my f-string 这个 Python function 中的 f 弦是如何工作的? - How does the f-string in this Python function work? 为什么错误:SyntaxError: f-string: mismatched '(', '{', or '[' in a block of code发生在 Python 中? - Why does the error: SyntaxError: f-string: mismatched '(', '{', or '[' in a block of code occur in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM