stringName.上()仅用python大写字母C

2024-10-03 00:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

阿洛, 我一直在尝试用python制作一个秒表/倒计时计时器,当我输入我的原始输入来选择什么类型的计时器(秒表或倒计时)时,当我输入时,它会将C(表示倒计时)转换为upcase,但当我输入S(表示秒表)时,它不会将它转换为upcase。我试过写几封不同的信,但也不管用。这是我的密码:

typeOfTimer = raw_input("What type of timer do you want? enter c for countdown and s for stopwatch. ")
typeOfTimer.upper()

print typeOfTimer

if typeOfTimer == "C":
    countdown()
elif typeOfTimer == "S":
    stopwatch()
else:
    print "Invalid type"

有人知道怎么修吗?你知道吗


Tags: 密码类型forinputrawtypewhat计时器
2条回答

.upper()从旧字符串创建新字符串。它不会改变调用方法的字符串。请尝试以下操作:

typeOfTimer = typeOfTimer.upper()

.upper()不修改变量。相反,用raw_input()来指定它:

typeOfTimer = raw_input("What type of timer do you want? enter c for countdown and s for stopwatch. ").upper()

以下是您的更新代码:

typeOfTimer = raw_input("What type of timer do you want? enter c for countdown and s for stopwatch. ").upper()

print typeOfTimer

if typeOfTimer == "C":
    countdown()
elif typeOfTimer == "S":
    stopwatch()
else:
    print "Invalid type"

如您所见,除非赋值,否则仅仅调用.upper()不会更改变量。你知道吗

>>> x = 'c'
>>> x.upper()
'C'
>>> x
'c'
>>> x = x.upper()
>>> x
'C'
>>>

相关问题 更多 >