为什么.capitalize方法在这里不起作用?

时间:2018-02-01 05:10:28

标签: python-3.x

这是我在python中的代码 This is the whole program in gist.

import random
    list= [ "Rock", "Paper", "Scissors" ]
    my_decision = random.choice(list)
    while True:
        game = input("Let's play Rock-Paper-Scissors Game! Please enter your decision: ")
        game.capitalize() 

此方法不起作用。当我进入摇滚,纸张或剪刀时,它只是没有把第一个字母大写。

2 个答案:

答案 0 :(得分:0)

您可以使用.title()

game = game.title()
print(game)

如果您将"rock"打印出来"Rock",如果您输入"two words",则上面的脚本会打印出"Two Words"

答案 1 :(得分:0)

这里的方法很好用。您只是没有将结果分配给game

while True:
    game = input("Let's play Rock-Paper-Scissors Game! Please enter your decision: ")
    game = game.capitalize() # You need to assign to `game`

但更重要的是while True是一个无限循环。除非有更多代码没有显示。如果没有,请确保在需要时添加break以退出循环。

相关问题