randint in Python 3.5 doesn't work [duplicate] - python

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 7 years ago.
I am a new to Python, and I ran into some issues when developing a D&D Dice program.
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!")
When I run the program and enter the number of dice, it always enter the first if statement.

You want to write:
if dice=="D4" or dice=="d4":
instead of:
if dice=="D4" or "d4":
"d4" by itself has a (true) boolean value because it isn't an empty string.

Related

Beginner - While Loop Troubles [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 1 year ago.
For my studies, I have begun to learn the Python Programming Language.
Fairly new to programming and having some trouble understanding why this won't work.
while compReady == False:
compAI = input("Which strategy for the computer [1,2,3]? ")
if compAI == "1" or "2" or "3":
compReady = True
elif compAI != "1" or "2" or "3":
print("Please enter either 1, 2, or 3.")
The problem I have is that no matter what is inputted into compAI, it runs through the 'if' statement. Any help is appreciated, thanks.
if condition should be written as ..
if compAI == ("1" or "2" or "3"):
In your code, Python interpreter will treat "2" and "3" as true. Please note it is not compared with compAI as per if statement syntax.

How to exit a loop? [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
I'm asking input from the user and wanted to check if he used the right letters to continue, I've wrote this code, but even when I write "y" or "n", it doesn't exit the loop, and I have no idea why:
while True:
start = input("Want to start? (Y/n) ").lower()
if start != 'y' or 'n':
print("letter not accepted! Please write 'Y' to start or 'N' to exit!\n")
else:
break
if start == "y":
separate(players)
else:
print("Goodbye!")

Python If statement and logical operator issue [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
I'm trying to create a small python program to emulate rolling a single die. My problem arises in that I want the program to accept both upper and lower-case 'Y' to trigger a re-roll. My program works when I accept either upper or lower case y but when I try to change the line
if usr == 'Y'
to
if usr == 'Y' or 'y'
creates a problem where it never enters the else statement, making the program never terminate.
import random
def DiceRoll():
number = random.randrange(1, 7, 1)
print("Dice Roll: ", number)
AskUser()
return
def AskUser():
usr = input("Roll Again? (Y/N) \n")
if usr == 'Y':
DiceRoll()
else :
print("Thank you for playing!")
return
DiceRoll()
You must do
if usr == 'Y' or usr == 'y'.
It is currently evaluating the statement 'y' as its own condition, and in this case 'y' always evaluates to true.

Python while loop breaks in every case [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 6 years ago.
I'm trying to make a program that rolls a dice and checks if the user wants to continue every roll, if not, the program should halt. Although, no matter what you input, the program breaks out of the loop. Can someone explain why and give me some tips to making a program that is simpler and works? Thanks
import random
sideNumber = int(input("Enter the number of sides in the die: "))
print("Dice numbers: ")
while True:
print(random.randint(0, sideNumber))
print("Do you want to continue?")
response = input()
if response == "n" or "no":
break
That's because the statement "no" is still true.
You should do :
if response == "n" or response == "no":
or better :
if response in ["n", "no"] :
if response == "n" or "no":
makes your code fail. This checks if the boolean value of "no" is true, and it always is. replace it with:
if response == "n" or response == "no":

Why is my program doing this? [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
For some reason my program keeps rolling the dice again regardless of the users answer, and it doesn't print the goodbye message.
I'll include the code below so you can test yourself (Python 3)
from random import randint
def rollthedice():
print("The number you rolled was: "+ str(randint(1,6)))
rollthedice()
shouldwecontinue = input("Do you wish to roll again? (y or n) ").lower()
if shouldwecontinue == "y" or "yes":
rollthedice()
elif shouldwecontinue == "n" or "no":
print("Goodbye.")
elif shouldwecontinue != type(str):
print("Sorry the program only accepts 'y' or 'n' as a response.")
The desired effect of the program should be that if the user enters y, it rolls the dice again, respectively starting the whole program again. Whereas, if the user enters no then it should print the Goodbye message.
or doesn't work like that. Right now your program is evaluating (shouldwecontinue == "y") or "yes", and since Python interprets "yes" as truthy, then that condition always succeeds. The simplest fix is to change it to shouldwecontinue == "y" or shouldwecontinue == "yes". And then similar changes for the other conditional expressions, of course.

Categories

Resources