So here we are packing objects and Both Length, Width and Height of object should be less than 50 for a good case and if any is more than 50 then the case is bad. I have written my program below but all cases are judged by the last case.
Sample input
2
20 20 20
1 2 51
Sample output
Case 1: good
Case 2: bad
T = int(input("Enter the number of test cases:\n"))
for i in range(1,T+1):
print("Enter the values of the Length, Width, Height")
case = input()
#Length_1.
if case[1] == " ":
L = int(case[0])
#Width and heigth.
if case[3] == " " and len(case) == 5:
W = int(case[2])
H = int(case[4])
if case[4] == " " and len(case) == 6:
W = int(case[2:4])
H = int(case[5])
if case[3] == " " and len(case) == 6:
W = int(case[2])
H = int(case[4:6])
if case[4] == " " and len(case) == 7:
W = int(case[2:4])
H = int(case[5:7])
#Length_2.
if case[2] == " ":
L = int(case[0:2])
#Width and height.
if case[4] == " " and len(case) == 6:
W = int(case[3])
H = int(case[5])
if case[5] == " " and len(case) == 7:
W = int(case[3:5])
H = int(case[6])
if case[4] == " " and len(case) == 7:
W = int(case[3])
H = int(case[5:7])
if case[5] == " " and len(case) == 8:
W = int(case[3:5])
H = int(case[6:8])
for i in range(1,T+1):
if L > 50 or W > 50 or H > 50:
print("Case ", i,":"," bad", sep='')
else:
print("Case ", i,":"," good", sep='')
If your problem is that all cases are judged by the properties of the last case (though, as has been noted, it is not clear what this means for the results of your code as given), then it could be because when case is assigned in:
T = int(input("Enter the number of test cases:\n"))
for i in range(1,T+1):
print("Enter the values of the Length, Width, Height")
case = input()
it is assigned anew during each iteration. Consequently, the previous input gets lost.
Edit:
The following code solves your problem by turning cases into a list:
T = int(input("Enter the number of test cases:\n"))
cases = []
for i in range(1,T+1):
print("Enter the values of the Length, Width, Height")
dimensions = input()
c = dimensions.split(" ")
cases.append(c)
for j, case in enumerate(cases):
if int(case[0]) > 50 or int(case[1]) > 50 or int(case[2]) > 50:
print("Case ", j,":"," bad", sep='')
else:
print("Case ", j,":"," good", sep='')
In this suggestion, the user input is automatically split into the different values using split() instead of manually doing it via if statements as it has been the case in your original post (this snippet assumes that the input provides values separated by a single space).
Related
I am a beginner in Python and I think I need some help with my program. Any kind of help or advice would be appreciated:)
You can see the program below, when I run it it gets stuck on the part of comparing the random ticket with the winning ticket(win_combination).
from random import choice
#Winning ticket
win_combination = input("Enter the winning combination of 4 numbers(1-digit-numbers): ")
while len(win_combination) != 4:
if len(win_combination) > 4:
win_combination = input("Reenter a shorter combination(4 one-digit-numbers): ")
elif len(win_combination) < 4:
win_combination = input("Reenter a longer combination(4 one-digit-numbers): ")
print(f"Winning combination is {win_combination}.")
#Specifying range of numbers to choose from
range = range(0, 10)
#Making a fake comparison-ticket to start of the loop
random_ticket = [0, 0]
random_ticket_string = f"{random_ticket[0]}{random_ticket[1]}{random_ticket[2]}{random_ticket[3]}"
#Params for the loop
n_tries = 0
n_guesses = 1
while random_ticket_string != win_combination:
while n_tries > 4:
random_ticket.clear()
number = choice(range)
random_ticket.append(number)
n_tries += 1
n_guesses += 1
random_ticket_string = f"{random_ticket[0]}{random_ticket[1]}"
if random_ticket_string == win_combination:
chance_to_win = f"{(1 / n_guesses) * 100}%"
print("Estimated percent to win is " + chance_to_win + ", it took " + f"{n_guesses} to match the winning combination.")
else:
n_tries = 0
I need my program to generate as many playing fields as the entered number of players playing the game. Currently it only generates 1 field. Cant wrap my head around this one.
s = int(input("Sisesta mängijate arv: "))
b = int(input("Sisesta väjaku suurus: "))
b = b + 1
bb = b
c = int(input("Sisesta korrutustabeli suurus: "))
for s in range (1 , s+1):
numberolemas = ['0']
t.write(str(s) + ". väljak \n\n")
print(str(s) + ". väljak \n")
for bb in range (1 , bb):
for b in range (1 , b):
import random
number = random.randint(1, c) * random.randint(1, c)
olemas = True
while olemas:
number = random.randint(1, c) * random.randint(1, c)
if number not in numberolemas:
olemas = False
t.write(str(number))
print(str(number), end = ' ')
if number <= 9:
t.write(" ")
print(" ", end = '')
elif number >= 100:
t.write(" ")
print(" ", end = '')
else: t.write(" ") and print(" ", end = '')
numberolemas.append(number)
b = b + 1
t.write("\n\n")
print(" ")
bb = bb + 1
print(" ")
t.close() ```
I want to create a simple game in which two players "roll the dice against each other". Winner is whoever gets one number three times in a row. I tried many different ways but in the end I always struggled with the evaluation.
How can I determine which player got one specific number 3-times in a row? Thanks for your advice!
import random
#Initialisieren der Variablen
wurf1_1 = 0
wurf2_1 = 0
gewuerfelt_s1 = []
gewuerfelt_s2 = []
n = 1
while (True):
#Bestimmen der Augenzahlen des Würfels
wurf1_1 = random.randint(1,6)
wurf2_1 = random.randint(1,6)
print("Spiel " + str(n) + ":\tSpieler 1: " + str(wurf1_1) + "; Spieler 2: " + str(wurf2_1))
gewuerfelt_s1.append(wurf1_1)
gewuerfelt_s2.append(wurf2_1)
wurf1_2 = random.randint(1,6)
wurf2_2 = random.randint(1,6)
n += 1
print("Spiel " + str(n) + ":\tSpieler 1: " + str(wurf1_2) + "; Spieler 2: " + str(wurf2_2))
if (wurf1_2 == gewuerfelt_s1[0]):
gewuerfelt_s1.append(wurf1_2)
wurf1_3 = random.randint(1,6)
n += 1
print("Spiel " + str(n) + ":\tSpieler 1: " + str(wurf1_3) + "; Spieler 2: " + str(wurf2_2))
if wurf1_3 == gewuerfelt_s1[1]:
print("Spieler 1 hat dreimal hintereinander die Zahl", gewuerfelt_s1[1], "gewürfelt. Sieger!")
break
else:
del gewuerfelt_s1[:]
continue
else:
del gewuerfelt_s1[:]
continue
Don't delete elements from your list. You can check the most recently appended elements of a list by indexing from the end
The last element of a list is:
my_list[-1]
The second last is:
my_list[-2]
So for each roll after the second roll, you can check:
my_list[-1] == my_list[-2] == my_list[-3]
(Can't comment, low rep)
First of all, make a function that will simulate the dice, it should return an integer number between [1,6] and should be generated using easily available (pseudo)random functions.
Once this is done, Declare variables, continous1, continous2, prev1, prev2.
The prev variables would store the prev dice role answer for that player if the current turn is the same as the prev for that player, increasing the continuous count. The first to reach the 3 is your answer. Use a while loop to simulate this.
Here is the code
import random
continous1 = 0
continous2 = 0
prev1 = 0
prev2 = 0
while continous1 != 3 and continous2 != 3:
person1 = random.randint(1,6)
person2 = random.randint(1,6)
if person1 == prev1:
continous1 += 1
else:
continous1 = 0
if person2 == prev2:
continous2 += 1
else:
continous2 = 0
prev1 = person1
prev2 = person2
# Note that even if both persons win the game at the same time, the first person will be chosen as the winner
if continous1 == 3:
print("Person 1 won the game")
else:
print("Person 2 won the game")
Check this pseudo code
PlayGame()
{
bool gameover = false;
int turns ;
player1_values = []
player2_values = []
While(!gameover)
{
player1_values.Add(getrandon(1,6));
player2_values.Add(getrandon(1,6));
bool player1_won = Check(player1_values);
if(!player1_won && player1_values.Count == 3)
player1_values.Clear();
bool player2_won = Check(player2_values);
if(!player1_won && player2_values.Count == 3)
player2_values.Clear();
gameover = player1_won || player2_won;
}
}
Check(values)
{
if(values.Count < 3)
return false;
int num = values[0];
for(int i = 1;i<3;i++)
{
if(values[i] != num)
return false;
}
return true;
}
I'm am beginning my journey into the world of python. I have done a couple of small projects, but the game of life really piqued my interest. Unfortunately, when I tried to replicate the game, everything worked except for the death of my cells. Per the rules, if a live cell has less than 2 neighbors, or more than 3, it should die. Cells are being born, but alas, they seem to be immortal. Can anyone spot my mistake? I'm including the entire code because I have no idea where I went wrong.
import random, time, copy
height = 10
width = 10
next = []
for x in range(width):
column = []
for y in range(height):
if random.randint(0, 1) == 0:
column.append(" ")
else:
column.append("#")
next.append(column)
while True:
print("\n\n\n\n")
current = copy.deepcopy(next)
for y in range(height):
for x in range(width):
print(current[x][y], end=" ")
print()
for x in range(width):
for y in range(height):
leftco = (x - 1) % width
rightco = (x + 1) % width
botco = (y - 1) % height
topco = (y + 1) % height
neighbors = 0
if current[leftco][topco] == "#":
neighbors = neighbors + 1
if current[leftco][y] == "#":
neighbors = neighbors + 1
if current[leftco][botco] == "#":
neighbors = neighbors + 1
if current[x][topco] == "#":
neighbors = neighbors + 1
if current[x][botco] == "#":
neighbors = neighbors + 1
if current[rightco][topco] == "#":
neighbors = neighbors + 1
if current[rightco][y] == "#":
neighbors = neighbors + 1
if current[rightco][botco] == "#":
neighbors = neighbors + 1
if current[x][y] == "#" and (neighbors == 2 or neighbors == 3):
next[x][y] = "#"
elif current[x][y] == " " and neighbors == 3:
next[x][y] == "#"
else:
next[x][y] == " "
time.sleep(1)
In this section, you confused == and = when you try to assign to a value of next[x][y]
if current[x][y] == "#" and (neighbors == 2 or neighbors == 3):
next[x][y] = "#"
elif current[x][y] == " " and neighbors == 3:
next[x][y] == "#"
else:
next[x][y] == " "
So it should be:
if current[x][y] == "#" and (neighbors == 2 or neighbors == 3):
next[x][y] = "#"
elif current[x][y] == " " and neighbors == 3:
next[x][y] = "#"
else:
next[x][y] = " "
By the way, you see next is highlighted as a special name. That's because next() is a special function in Python standard library, and you shouldn't name your own variables like that. It's harmless in your current program, because you don't use it, but develop a habit to use more specific names, that don't shadow the standard ones
I am working on a Hangman game, but I am having trouble replacing the dashes with the guessed letter. The new string just adds on new dashes instead of replacing the dashes with the guessed letter.
I would really appreciate it if anyone could help.
import random
import math
import os
game = 0
points = 4
original = ["++12345","+*2222","*+33333","**444"]
plusortimes = ["+","*"]
numbers = ["1","2","3"]
#FUNCTIONS
def firstPart():
print "Welcome to the Numeric-Hangman game!"
def example():
result = ""
ori = random.choice(original)
for i in range(2,len(ori)):
if i % 2 == 0:
result = result + ori[i] + ori[0]
else:
result = result + ori[i] + ori[1]
return ori
# def actualGame(length):
#TOP LEVEL
firstPart()
play = raw_input("Do you want to play ? Y - yes, N - no: ")
while (play == "Y" and (points >= 2)):
game = game + 1
points = points
print "Playing game #: ",game
print "Your points so far are: ",points
limit = input("Maximum wrong guesses you want to have allowed? ")
length = input("Maximum length you want for the formulas (including symbols) (must be >= 5)? ")
result = "" #TRACE
ori = random.choice(original)
for i in range(2,len(ori)):
if i % 2 == 0:
result = result + ori[i] + ori[0]
else:
result = result + ori[i] + ori[1]
test = eval(result[:-1])
v = random.choice(plusortimes) #start of randomly generated formula
va = random.choice(plusortimes)
formula = ""
while (len(formula) <= (length - 3)):
formula = formula + random.choice(numbers)
formula2 = str(v + va + formula)
kind = ""
for i in range(2,len(formula2)):
if i % 2 == 0:
kind = kind + formula2[i] + formula2[0]
else:
kind = kind + formula2[i] + formula2[1]
formula3 = eval(kind[:-1])
partial_fmla = "------"
print " (JUST TO TRACE, the program invented the formula: )" ,ori
print " (JUST TO TRACE, the program evaluated the formula: )",test
print "The formula you will have to guess has",length,"symbols: ",partial_fmla
print "You can use digits 1 to 3 and symbols + *"
guess = raw_input("Please enter an operation symbol or digit: ")
a = 0
new = ""
while a<limit:
for i in range(len(formula2)):
if (formula2[i] == partial_fmla[i]):
new = new + partial_fmla[i]
elif (formula2[i] == guess):
new[i] = guess
else:
new[i] =new + "-"
a = a+1
print new
guess = raw_input("Please enter an operation symbol or digit: ")
play = raw_input("Do you want to play ? Y - yes, N - no: ")
The following block seems problematic:
elif (formula2[i] == guess):
new[i] = guess
else:
new[i] =new + "-"
Python does not allow modification of characters within strings, as they are immutable (cannot be changed). Try appending the desired character to your new string instead. For example:
elif formula2[i] == guess:
new += guess
else:
new += '-'
Finally, you should put the definition of new inside the loop directly under, as you want to regenerate it after each guess.