简体   繁体   English

没有错误时出现错误代码

[英]error code comes up when no error

I'm trying to make a error message for my dice sim 我正在尝试为我的骰子SIM卡生成错误消息

import random
loop=1

while loop == 1:
 dice=input("Choose dice 4,6 or 12 sided")
 if dice =="4":
     n=random.randint(1,4)
     print(dice)
     print(n)
 if dice =="6":
     n=random.randint(1,dice)
     print(dice)
     print(n)
 if dice =="12":
     n=random.randint(1,dice)
     print(dice)
     print(n)

 else:
    print("Error")

error comes up for 4 and 6 but when i use the 12 sided no error comes up 4和6出现错误,但是当我使用12面时,没有错误出现

Choose dice 4,6 or 12 sided4
4
4
Error

You should really state which programming language you're using. 您应该真正说明所使用的编程语言。 I'm assuming this is Python, but if it's not my answer might be wrong. 我假设这是Python,但是如果不是,我的答案可能是错误的。

Your problem is that you need to be using elif , not if. 您的问题是您需要使用elif ,而不是if。 You're also trying to implicitly convert between strings and integers, which doesn't work. 您还试图在字符串和整数之间进行隐式转换,这是行不通的。 This code should, unless I've missed something else. 除非我错过了其他事情,否则这段代码应该会。

import random
loop=1

while loop == 1:
 dice=input("Choose dice 4,6 or 12 sided")
 if dice =="4":
     n=random.randint(1,4)
     print(dice)
     print(str(n))
 elif dice =="6":
     n=random.randint(1,int(dice))
     print(dice)
     print(str(n))
 elif dice =="12":
     n=random.randint(1,int(dice))
     print(dice)
     print(str(n))
 else:
    print("Error")

You need to use elif instead of if, or a switch statement. 您需要使用elif代替if或switch语句。

The code you provided says "if dice does not equal 12 then print Error". 您提供的代码显示“如果骰子不等于12,则显示错误”。

Try something like: 尝试类似:

while loop == 1:
 dice=input("Choose dice 4,6 or 12 sided")
 if dice =="4":
     n=random.randint(1,4)
     print(dice)
     print(n)
 elif dice =="6":
     n=random.randint(1,dice)
     print(dice)
     print(n)
 elif dice =="12":
     n=random.randint(1,dice)
     print(dice)
     print(n)
 else:
    print("Error")

This lets you break out of the loop early without evaluating every single expression. 这使您可以尽早摆脱循环,而无需评估每个表达式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我创建一个Class对象时出现类型错误 - Type Error comes up when I create a Class object 尝试在出现该错误时将 discord 重命名权限错误转换为默认消息,我该怎么做? - Trying To Convert a discord rename permission error to a default message when that error comes up, How Do I do That? 为什么python,当我运行我的程序时出现错误'预期缩进块? - Why does python, when I run my program comes up with the error 'expected an indented block? 我的登录方法未在我的register()中调用,并且在mainError(0)中出现我的错误,当不应该这样做时 - my login method is not being called within my register() and my error in mainError(0 comes up when it's not supposed to 使用 input() 函数时出现 Python 错误 [TypeError: 'str' object is not callable] - Python Error [TypeError: 'str' object is not callable] comes up when using input() funtion 我正在尝试制作脚本并且出现此错误 - I'm trying to make a scripts and this error comes up 尝试在python中添加值,但出现int不可调用错误 - Trying to add values in python but comes up with int not callable error 尝试安装 pyInstaller,但是在创建 venv 时出现错误提示“系统找不到指定的路径”。 - Trying to install pyInstaller, however when creating a venv comes up with error saying “The system cannot find the path specified.” Django - 条纹。 您没有设置有效的可发布密钥。 尝试在 django 网站上合并条带付款时出现的错误 - Django - Stripe. You did not set a valid publishable key. Error that comes up when trying to incorprate stripe payments on a django website 我已经在 jupyter notebook 中安装了 BeautifulSoup4。 尝试使用“from bs4 import BeautifulSoup”时出现错误 - I've installed BeautifulSoup4 in jupyter notebook. Error comes up when trying to use "from bs4 import BeautifulSoup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM