【问题标题】:randint in Python 3.5 doesn't work [duplicate]Python 3.5中的randint不起作用[重复]
【发布时间】:2015-12-27 09:55:18
【问题描述】:

我是 Python 新手,在开发 D&D Dice 程序时遇到了一些问题。

import random   

print("Hi,here you can roll all D´n´D Dice´s!")  
dice=input("What dice do u want?D4,D6,D8,D10,D12,D20 or D100?")  

if dice=="D4" or "d4":  
    print(random.randint(1,4))  
elif dice=="D6" or "d6":  
    print(random.randint(1,6))  
elif dice=="D8" or "d8":  
    print(random.randint(1,8))  
elif dice=="D10"or"d10":  
    print(random.randint(1,10))  
elif dice=="D12"or"d12":  
    print(random.randint(1,12))      
elif dice=="D20"or"d20":  
    print(random.randint(1,20))  
elif dice=="D100"or"d100":  
    print(random.randint(1,100))  
else: print("No?Ok!")  

当我运行程序并输入骰子的数量时,它总是输入第一个if语句。

【问题讨论】:

  • 你总是得到一个从 1 到 4 的数字(正如 Baruchel 的回答所修正的那样),还是你总是得到数字 1?另外,如果输入无效的输入(例如,D3?)会得到什么

标签: python python-3.5


【解决方案1】:

你想写:

if dice=="D4" or dice=="d4":

代替:

if dice=="D4" or "d4":

"d4" 本身有一个(真)布尔值,因为它不是空字符串。

【讨论】:

  • dice.lower() == 'd4'
猜你喜欢
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 2015-04-30
  • 2018-12-22
相关资源
最近更新 更多