01/10/2018, 09:04

In f-string python theo Learn Python the Hard Way

https://learnpythonthehardway.org/book/ex24.html
trong đó có 1 đoạn code nó in ra như vậy?

We can also do that this way:
We'd have 500000 beans, 500 jars, and 5 crates.

đây là đoạn code mình sửa lại theo pep 498

print ("We can also do that this way:")
print (f"We'd have {secret_formula(start_point)} beans, {secret_formula(start_point)} jars, and {secret_formula(start_point)} crates.") 

nó lại in ra như vậy

We can also do that this way:
We'd have (500000, 500, 5) beans, (500000, 500, 5) jars, and (500000, 500, 5) crates.
Nguyễn Hồng Quân viết 11:05 ngày 01/10/2018

Đoạn code trước khi print:

start_point = 10000
beans, jars, crates = secret_formula(start_point)
start_point = start_point / 10

Nên nếu bạn dùng

f"We'd have {secret_formula(start_point)} beans"

thì nó sẽ chạy code

secret_formular(1000)

Và hàm secret_formular thì trả về một tuple 3 phần tử, nên bạn nhận được (500000, 500, 5) là đúng rồi.

Bài liên quan
0