【问题标题】:How can I make a word as an answer in int(input);如何在 int(input) 中将单词作为答案;
【发布时间】:2019-03-02 00:33:35
【问题描述】:
import time
import random

lista=('ACE','2','3','4','5','6','7','8','9','10','BOY','LADY','KING')
print('You have got',random.choice(lista))
i=int(input('Do you want to get a new card'))
YES=1
if i == YES:
    print('Your second card is:',random.choice(lista))

我想以YES 作为答案,但它不起作用。你能解释一下我该怎么做吗?

【问题讨论】:

  • 什么不起作用?您是在提示符下输入YES 还是1

标签: python if-statement input int


【解决方案1】:

试试这个:

import time
import random
lista=('ACE','2','3','4','5','6','7','8','9','10','BOY','LADY','KING')
print('You have got',random.choice(lista))

i = input('Do you want to get a new card: ')

if i == "YES":
    print('Your second card is:',random.choice(lista))

您使用“是”(字符串)作为整数,您无法将整数与字符串进行比较。

【讨论】:

  • 这种比较永远不会发生,因为您首先无法将"YES" 转换为int
【解决方案2】:

这会奏效

import time
import random

lista=('ACE','2','3','4','5','6','7','8','9','10','BOY','LADY','KING')
print('You have got',random.choice(lista))

i = input('Do you want to get a new card: ')

if i == "YES":
    print('Your second card is:',random.choice(lista))

请将i 作为str 输入,然后应用if 操作。

【讨论】:

  • 你不需要调用str,因为input已经返回了str
  • 我已经改了@chepner
猜你喜欢
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
  • 2016-03-25
  • 1970-01-01
相关资源
最近更新 更多