Trong dòng cuối cùng của bạn để tính toán chi phí:
subtotal=[askSize + askBeverage + askFlav]
Bạn không tổng hợp các biến bạn đã xác định ở đầu tệp của mình [với chi phí], nhưng bạn kết hợp các chuỗi mà người dùng đã nhập thay thế. Chẳng hạn askSize
có thể là chuỗi "nhỏ". Vì vậy, những gì bạn làm ở đây có thể được viết như:
subtotal=["small" + "tee" + "lemon"]
Trong Python, sử dụng +
giữa các chuỗi sẽ kết hợp chúng. Nó có nghĩa là nó sẽ thêm các chuỗi lần lượt để tạo ra một chuỗi lớn hơn. Ví dụ:
subtotal=["small" + "tee" + "lemon"]
0.Nếu bạn muốn tham khảo chi phí của từng tùy chọn, một giải pháp [nhưng có những giải pháp khác] sẽ là sử dụng từ điển để lưu trữ chi phí thay vì một số biến như bạn đã làm:
# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
Sau đó, để có được số tương ứng với một chuỗi, bạn nhận được các mục từ từ điển:
tea_cost = costs["tea"] # tea_cost will be equal to 1.50
ask_size_cost = costs[askSize] # ask_size_cost will be equal to the cost of the asked size
Bây giờ, thật dễ dàng để có được tổng chi phí của bạn:
total_cost = costs[askSize] + costs[askBeverage] + costs[askFlav]
Tất nhiên, chăm sóc các trường hợp chuỗi, mọi thứ phải là chữ thường trong ví dụ của tôi.
Hy vọng nó giúp !
NB: Bạn có thể thực hiện nhiều cải tiến cho phần còn lại của mã của mình, đừng ngần ngại đọc một số mã Python trên Internet để làm cho nó tốt hơn.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận The simplest way in which this task can be performed is by converting the integer explicitly into string datatype using the basic type conversion and adding it to appropriate position.
Python3
subtotal=["small" + "tee" + "lemon"]
1subtotal=["small" + "tee" + "lemon"]
2 subtotal=["small" + "tee" + "lemon"]
3subtotal=["small" + "tee" + "lemon"]
4subtotal=["small" + "tee" + "lemon"]
2 subtotal=["small" + "tee" + "lemon"]
6Đôi khi, trong khi xử lý các chuỗi, chúng ta có thể gặp phải một vấn đề trong đó chúng ta có thể có một biến số có giá trị tiếp tục thay đổi và chúng ta cần in chuỗi bao gồm cả số đó. Chuỗi và số là các loại dữ liệu khác nhau phải được giải quyết theo những cách khác nhau. Hãy để thảo luận về những cách nhất định trong đó vấn đề này có thể được giải quyết. & NBSP;
Phương pháp số 1: Sử dụng chuyển đổi loại Cách đơn giản nhất trong đó nhiệm vụ này có thể được thực hiện là bằng cách chuyển đổi số nguyên một cách rõ ràng thành kiểu dữ liệu chuỗi bằng cách sử dụng chuyển đổi loại cơ bản và thêm nó sang vị trí thích hợp. & NBSP;
subtotal=["small" + "tee" + "lemon"]
7subtotal=["small" + "tee" + "lemon"]
8subtotal=["small" + "tee" + "lemon"]
9 # Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
0+
# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
2subtotal=["small" + "tee" + "lemon"]
7# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
4+
# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
6# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
7
8# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
2 subtotal=["small" + "tee" + "lemon"]
1subtotal=["small" + "tee" + "lemon"]
+
6# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
3tea_cost = costs["tea"] # tea_cost will be equal to 1.50
ask_size_cost = costs[askSize] # ask_size_cost will be equal to the cost of the asked size
+
5tea_cost = costs["tea"] # tea_cost will be equal to 1.50
ask_size_cost = costs[askSize] # ask_size_cost will be equal to the cost of the asked size
The original string is : Geeks The original number : 4 The string after adding number is : Geeks4Geeks
________ 17 ________ 37 ________ 19 & nbsp;
0____9 # Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
6# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
2 This operator can be used to format the string to add the integer.
The “d” represents that the datatype to be inserted to string is an integer. This can be changed according to the requirements. total_cost = costs[askSize] + costs[askBeverage] + costs[askFlav]
Python3
subtotal=["small" + "tee" + "lemon"]
1subtotal=["small" + "tee" + "lemon"]
2 subtotal=["small" + "tee" + "lemon"]
3subtotal=["small" + "tee" + "lemon"]
4subtotal=["small" + "tee" + "lemon"]
2 subtotal=["small" + "tee" + "lemon"]
6Đôi khi, trong khi xử lý các chuỗi, chúng ta có thể gặp phải một vấn đề trong đó chúng ta có thể có một biến số có giá trị tiếp tục thay đổi và chúng ta cần in chuỗi bao gồm cả số đó. Chuỗi và số là các loại dữ liệu khác nhau phải được giải quyết theo những cách khác nhau. Hãy để thảo luận về những cách nhất định trong đó vấn đề này có thể được giải quyết. & NBSP;
Phương pháp số 1: Sử dụng chuyển đổi loại Cách đơn giản nhất trong đó nhiệm vụ này có thể được thực hiện là bằng cách chuyển đổi số nguyên một cách rõ ràng thành kiểu dữ liệu chuỗi bằng cách sử dụng chuyển đổi loại cơ bản và thêm nó sang vị trí thích hợp. & NBSP;
subtotal=["small" + "tee" + "lemon"]
7subtotal=["small" + "tee" + "lemon"]
8subtotal=["small" + "tee" + "lemon"]
9 # Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
0+
# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
2subtotal=["small" + "tee" + "lemon"]
7# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
4+
# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
6# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
7
8# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
2 subtotal=["small" + "tee" + "lemon"]
1subtotal=["small" + "tee" + "lemon"]
+
6# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
3tea_cost = costs["tea"] # tea_cost will be equal to 1.50
ask_size_cost = costs[askSize] # ask_size_cost will be equal to the cost of the asked size
+
5tea_cost = costs["tea"] # tea_cost will be equal to 1.50
ask_size_cost = costs[askSize] # ask_size_cost will be equal to the cost of the asked size
The original string is : Geeks The original number : 4 The string after adding number is : Geeks4Geeks
________ 17 ________ 37 ________ 19 & nbsp;
0____9 # Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
6# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
2total_cost = costs[askSize] + costs[askBeverage] + costs[askFlav]
Python3
subtotal=["small" + "tee" + "lemon"]
1subtotal=["small" + "tee" + "lemon"]
2 subtotal=["small" + "tee" + "lemon"]
3subtotal=["small" + "tee" + "lemon"]
4subtotal=["small" + "tee" + "lemon"]
2 subtotal=["small" + "tee" + "lemon"]
6subtotal=["small" + "tee" + "lemon"]
7askSize
5askSize
6 +
# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
2subtotal=["small" + "tee" + "lemon"]
7askSize
55____91 +
# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
6# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
7+
5
subtotal=["small" + "tee" + "lemon"]
2+
7+
8+
9+
5
subtotal=["small" + "tee" + "lemon"]
2# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
6subtotal=["small" + "tee" + "lemon"]
03subtotal=["small" + "tee" + "lemon"]
7askSize
5subtotal=["small" + "tee" + "lemon"]
06 +
# Dictionary of costs
costs = {"tea": 1.50,
"coffee": 1.50,
"small": 0,
"medium": 0.75,
"large": 1.75,
"mint": 0.50,
"lemon": 0.25,
"chocolate": 0.75,
"vanilla": 0.25,
"none": 0
}
6total_cost = costs[askSize] + costs[askBeverage] + costs[askFlav]
2Đầu ra
The original string is : Geeks The original number : 4 The string after adding number is : Geeks4Geeks