英文:
Python not converting str to int
问题
我编写了这段代码来转换日期:
```python
while True:
d = input("日期:")
if "/" in d:
try:
x, y, z = d.split("/")
float(x)
int(y)
int(z)
print(type(x))
print(type(y))
print(type(z))
# print(f" {z : 03d} ")
# print(f" {x : 03d} ")
# print(f" {y : 03d} ")
break
except:
break
if "," in d:
x, y = d.split(",")
print(x, y)
但当我输入:
日期:9/8/2020
它没有将字符串转换为整数,输出结果为:
<class 'str'>
<class 'str'>
<class 'str'>
为什么??
预期的类别应该是整数。
<details>
<summary>英文:</summary>
i made this code to convert dates:
while True:
d = input("Date: ")
if "/" in d:
try:
x, y, z = d.split("/")
float(x)
int(y)
int(z)
print(type(x))
print(type(y))
print(type(z))
# print(f" {z : 03d} ")
# print(f" {x : 03d} ")
# print(f" {y : 03d} ")
break
except:
break
if "," in d:
x, y = d.split(",")
print(x,y)
but when i give an input of:
`Date: 9/8/2020`
it is not converting the strings to ints, output is:
`<class 'str'>`
`<class 'str'>`
`<class 'str'>`
WHY??
expecting the class type to be int
</details>
# 答案1
**得分**: 2
将以下代码从英文翻译成中文:
```python
x = float(x)
y = int(y)
z = int(z)
没有赋值语句的话,它只是评估表达式然后丢弃结果。
英文:
Change:
float(x)
int(y)
int(z)
to:
x = float(x)
y = int(y)
z = int(z)
Without the assignments, all it's doing is evaluating the expressions, then discarding the results.
答案2
得分: 0
Here is the translated code snippet:
while True:
d = input("日期:")
if "/" in d:
try:
x, y, z = d.split("/")
x = float(x)
y = int(y)
z = int(z)
print(type(x))
print(type(y))
print(type(z))
break
except:
break
if "," in d:
x, y = d.split(",")
print(x, y)
I have translated the code while preserving the original structure and comments.
英文:
while True:
d = input("Date: ")
if "/" in d:
try:
x, y, z = d.split("/")
x = float(x)
y = int(y)
z = int(z)
print(type(x))
print(type(y))
print(type(z))
# print(f" {z : 03d} ")
# print(f" {x : 03d} ")
# print(f" {y : 03d} ")
break
except:
break
if "," in d:
x, y = d.split(",")
print(x,y)
you are just converting not assigning it in to the variable
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论