即使You Win!
更高,代码也会继续打印computer_card
。
import random
computer_card = random.choice([2,3,4,5,6,7,8,9,10,11,12,13,14])
player_card = raw_input('Please pick a card \n')
if player_card == 'Jack' or player_card == 'jack':
player_card = 11
if player_card == 'Queen' or player_card == 'queen':
player_card = 12
if player_card == 'King' or player_card == 'king':
player_card = 13
if player_card == 'Ace' or player_card == 'ace':
player_card = 14
if computer_card == player_card:
print 'Computer Choice: ', computer_card
print 'Player Choice: ', player_card
print 'Tie!'
elif computer_card > player_card:
print 'Computer Choice: ', computer_card
print 'Player Choice: ', player_card
print 'Computer Wins!'
elif computer_card < player_card:
print 'Computer Choice: ', computer_card
print 'Player Choice: ', player_card
print 'You Win!'
else:
print 'I am not sure what is really going on if you got to this line.'
答案 0 :(得分:3)
问题:
raw_string()读取的输入转换为字符串。这一行,
Pre-main time: 0.52 seconds
App did launch to javascript did load time: 2.12 seconds
JS render time: 0.74 seconds
Total time: 3.34 seconds
读取输入,将其转换为字符串并将其放入player_card。并player_card=raw_input()
一种可能的解决办法:
如果输入不是['jack','queen','king'或'ace'],则更改比较流并将输入转换为int
'1' > 9
进一步改进:虽然此解决方案应解决当前问题,但代码可以大大改进。一些改进建议,
确保您的代码可以处理来自用户的无效输入[永不信任 用户输入]
使用dict或list将用户输入转换为数字 值而不是逐个处理案例。
答案 1 :(得分:0)
raw_input
将字符串放入变量player_card
。当玩家选择King,Queen,Ace,Jack时,player_card
语句将if
的值更改为int。但如果player_card
不是这些,player_card
中的值仍为字符串。 Like @Fallen said,可以通过将player_card
转换为int。