PYTHON - ΜΑΘΗΜΑ 8 - ΛΕΞΙΚΑ - Μέρος 9 από 9 - Project: Πέτρα-Ψαλίδι-Χαρτι

preview_player
Показать описание
ΠΕΡΙΕΧΟΜΕΝΑ ΒΙΝΤΕΟ
3. Project: Πέτρα – Ψαλίδι - Χαρτί
=====================================
ΠΕΡΙΕΧΟΜΕΝΑ ΜΑΘΗΜΑΤΟΣ
1. Λεξικά
1.1. Βασική Λειτουργικότητα Λεξικών
1.2. ‘Ελεγχος και μέθοδοι διαπέρασης
1.3. Περιγραφικά Λεξικά κ.α.
2. Debugging Μέρος 1: Εκτυπώνοντας…
3. Project: Πέτρα – Ψαλίδι - Χαρτί
Рекомендации по теме
Комментарии
Автор

Πολλά τα λεξικά δάσκαλεεεεεεεεεεεε! Ευχαριστούμε πολύ είσαι τρομερός, πάμε για συμβολοσειρές!

Kormoranos
Автор

Καλησπέρα και ευχαριστώ πολύ για τα μαθήματα που μας παρέχεται.
Θα ήθελα να ρωτήσω στη συγκεκριμένη άσκηση εάν δεν έκανα nested if, αλλά έγραφα 9 if (όσες και οι περιπτώσεις), και άνα If είχα Print(computer win) ή Print(user win) ή Print(tie). θα μπορούσα μετά να φτιάξω μετρητή του σκορ;
Ίσως είναι πολύ βασική ή απορία, αλλά είμαι πραγματικά αρχάριος.

araucanae
Автор

Σχετικά με την 9.4 αν χρησιμοποιήσω ένα λεξικό σαν το παρακάτω:

choices = {
"rock": 0,
"paper": 1,
"scissors": 2,
}

και το χρησιμοποιήσω για να βρώ την αφαίρεση ως εξής:
result = choices[user_choice] - choices[computer_choice]

είναι εντάξει?

manosmakris
Автор

Να κάνω μια ερώτηση, όταν βγάλαμε νικητή στις 3 νίκες, χρησιμοποιώντας το break, το πρόγραμμα βγενει γενικώς από το loop? Για να γίνω ποιο κατανοητός, έστω ότι έχουμε ένα nested loop με πολλαπλά if το ένα μέσα στο άλλο :
if.... :
:
:

Τότε εάν σε ένα ΕΣΩΤΕΡΙΚΌ if, το πρόγραμμα συνάντησει μια εντολή break, τότε τερματίζει ΟΛΌΚΛΗΡΟ το βρόχο? Από το πρώτο if? Παρόλο που το break βρίσκεται σε εσωτερικό if?
Ελπίζω να καταλάβατε την ερώτηση μου, επισηε συγχαρητήρια για την εκπληκτική δουλειά!

ΓιάννηςΝικολάου-μθ
Автор

Το συγκριμένο μάθημα είναι για Λεξικά και βλέπω ότι χρησιμοποιείτε λίστα, πως θα μπορούσαμε να φτιάχναμε το history ως λεξικό ? Δηλάδη αντι του history.append("Round " + str(round) + ": Player: " + player_input_str + ", Computer: " + computer_choice_str + ", Score: " + str(score[0]) + "-" + str(score[1])) θα μπορούσε να υλοποιηθεί με λεξικό ?

litomoschona
Автор

θα ειχε πλακα αν το καναμε αντίστοιχα με τα type advantages των ποκεμον (πχ charmander squirtle bulbasaur)

chrispan
Автор

Μια παρόμοια προσπάθεια. Το τελευταίο ερώτημα το δοκίμασα με dictionary διότι όταν όριζα τα player_int και upologistis_int μου πετούσε σφάλμα ότι δεν έχουν οριστεί κάπου.



from random import seed
from random import randrange
from datetime import datetime


game = ["petra", "psalidi", "xarti"]
wins_computer=0
wins_player=0
player = input("Paixe petra psalidi h xarti ")
round=0
history=[]
epiloges={
"petra":0,
"psalidi":1,
"xarti":2,

}

while True:
print(" ")
round += 1
print("Round: " + str(round))

while player not in game:
player = input("Paixe petra psalidi h xarti ")

pos = randrange(len(game))
upologistis =game[pos]

if player == "petra":
player_int=epiloges["petra"]

elif player =="psalidi":

else:
player_int=epiloges["xarti"]

if upologistis == "petra":
upologistis_int = epiloges["petra"]
elif upologistis == "psalidi":
upologistis_int = epiloges["psalidi"]
else:
upologistis_int = epiloges["xarti"]

print("Apotelesma afaireshs"

print("Player : "+str(player))
print("Computer: "+str(upologistis))


if player_int-upologistis_int == 1 or player_int-upologistis_int == -2:
wins_computer += 1
elif player_int-upologistis_int == 2 or player_int-upologistis_int == -1:
wins_player += 1
else:
print("No winner!")
continue


history.append("Round " + str(round) + ": Player: " + player + ", Computer: " + upologistis + ", Score: " + str(wins_player) + \
"-" + str(wins_computer))

if wins_player == 3:
print("Player won the game!")
print(" ")
for history_item in history:
print(history_item)
break
elif wins_computer ==3:
print("Computer won!")
print(" ")
for history_item in history:
print(history_item)
break

ΝάσοςΜπαλαχτσής
Автор

Ήθελα να προσπαθήσω αρκετά ώστε να φτάσω σε μια πιο advanced λύση που να περιέχει μαθηματικούς τύπους και ενώ κάηκα εντελώς νομίζω εν τέλει τα κατάφερα. θα ήθελα οπότε να μου πείτε τη γνώμη σας για τον παρακάτω κώδικα. Να τονίσω ότι δεν έβαλα την περίπτωση ισοπαλίας γιατί το θεώρησα λίγο ανούσιο.

from random import seed
from random import randrange
from datetime import datetime

choices = {
0: "Rock",
1: "Paper",
2: "Scissors",
}
history = []
players_score = [0, 0]
Round = 0
while True:
Round += 1
player_choice = int(input("Choose\n "
"0: Rock or\n "
"1: Paper or\n "
"2: Scissors:"))
while player_choice < 0 or player_choice > 2:
player_choice = int(input("Choose 0: Rock or 1: Paper or 2: Scissors:"))

computer_choice = randrange(0, 3)

while computer_choice == player_choice:
computer_choice = randrange(0, 3)

if abs(player_choice - computer_choice) == 1:
- computer_choice) - (player_choice - computer_choice)) // 2] += 1
else:
players_score[player_choice // 2] += 1


history.insert(0, "Round" + str(Round) + " Player: " + choices[player_choice] + " Computer: " + choices[
computer_choice] + " Score " + str(players_score[0]) + "-" + str(players_score[1]))
print("Round" + str(Round) + " Player: " + choices[player_choice] + " Computer: " + choices[
computer_choice] + " Score " + str(players_score[0]) + "-" + str(players_score[1]))
if 3 in players_score:
break

if players_score[0] > players_score[1]:
print("Player won")
else:
print("Computer won")
print(history)

Συγνώμη για τη στοίχιση, στο pycharm δεν φαίνεται έτσι

GeorgeT
Автор

from random import seed
from random import randrange


seed(a=None)


game = {
0: "rock",
1: "scissors",
2: "paper"
}

win_me = 0
win_computer = 0
round1 = 0
history = []
while (win_me < 3) and (win_computer < 3):
my_move = input("Rock, scissors or paper:")
while (my_move != "rock") and (my_move != "scissors") and (my_move != "paper"):
my_move = input("Rock, scissors or paper:")
computer_move = game[randrange(len(game))]
if (my_move == "rock" and computer_move == "scissors") or (my_move == "scissors" and computer_move == "paper") or (my_move == "paper" and computer_move == "rock"):
win_me += 1
elif (my_move == "rock" and computer_move == "paper") or (my_move == "scissors" and computer_move == "rock") or (my_move == "paper" and computer_move == "scissors"):
win_computer += 1
round1 += 1

print("Round " + str(round1) + ": Player: " + my_move + ", Computer: " + computer_move + ", Score: " + str(win_me) + "-" + str(win_computer))

history.append("Round " + str(round1) + ": Player: " + my_move + ", Computer: " + computer_move + ", Score: " + str(win_me) + "-" + str(win_computer))

if win_me == 3:
print("Player Won!")
else:
print("Computer Won")

print(history[1])
την κίνηση του παίχτη την έκανα εγώ καθε φορά αλλά την κίνηση του υπολογιστή την άφησα στην τύχη με το randrange όπως γίνεται και στην πραγματικότητα οταν παίζουμε πετρα-ψαλιδι-χαρτι με άλλο παιχτη

johnniewalkerjohnniewalker
Автор

ειναι καλή αυτή η κωδικοποίηση?


winning_conditions = {'rock' : 'scissors',
'scissors' : 'paper', #the key is the winner
'paper' : 'rock',
}

#check for winner

for k, v in winning_conditions.items(): #all elements in the dictionary
if k == player_choice and v == computer_choice: #when player has the winning key
print('You won!')
player_wins += 1
cnt += 1 #ignore round printing
cnt1 += 1 #variables
break
elif k == computer_choice and v == player_choice: #when cpu has the winning key
print('Computer won :(')
cpu_wins += 1
cnt += 1
cnt1 += 1
break
else:
print("It's a draw :/")
cnt += 1
cnt1 += 1

Jj-gisg
Автор

H προσεγγιση μου για το προβλημα του προτζεκτ!

from random import seed
from random import randrange
from datetime import datetime


player_wins = 0
computer_wins = 0
list_of_plays = ['Rock', 'Paper', 'Scissors']
round = 0
play_on = True
while play_on:
#This is the counter of the rounds that we play
round += 1
#This is the choice for the computer
computer_choice =

#This is the control and the choice for the player
player_pos = int(input('Plese give us the position from the list: '))
while player_pos > 2 or player_pos < 0:
player_pos = int(input('Please give us the correct position(0-2): '))
player_choice = list_of_plays[player_pos]

#This is the if statement with the control
if player_choice == computer_choice:
print('It is a draw, pick again both of you!')
print('')
continue
if player_choice == 'Rock' and computer_choice == 'Scissors':
player_wins += 1
print('Player wins!')
print('Player score:', str(player_wins), 'Computer_score: ', str(computer_wins))
print('')
elif player_choice == 'Scissors' and computer_choice == 'Paper':
player_wins += 1
print('Player Wins!')
print('Player score:', str(player_wins), 'Computer_score: ', str(computer_wins))
print('')
elif player_choice == 'Paper' and computer_choice == 'Rock':
player_wins += 1
print('Player Wins')
print('Player score:', str(player_wins), 'Computer_score: ', str(computer_wins))
print('')
else:
computer_wins += 1
print('Computer Wins!')
print('Player score:', str(player_wins), 'Computer_score: ', str(computer_wins))
print('')

history_dict = {'Round':round, 'Player':player_choice, 'Computer':computer_choice,
'Score:Player VS
print(history_dict)
print('')



if player_wins == 3:
print('This round goes to the player!')
('Player score:', str(player_wins), 'Computer_score: ', str(computer_wins))
break
elif computer_wins == 3:
print('This round goes to the computer!')
('Player score:', str(player_wins), 'Computer_score: ', str(computer_wins))
break
else:
pass

konstantinostzaferis
Автор

αρχικα εκανα το κανονικο και βγηκε ετσι

from random import seed
from random import randrange
from datetime import datetime

seed(datetime.now())

choices = ("rock", "paper", "scisors")
player_score = 0
computer_score = 0
round=1
while player_score!=3 or computer_score!=3:
choice = input("rock paper or scisors:")
while choice not in choices:
choice = input("rock paper or scisors:")
computer_choice = choices[randrange(0, 3)]
print("computer chose:"+computer_choice)
if choice== "rock" and computer_choice== "paper":
computer_score+=1
if choice == "rock" and computer_choice == "scisors":
player_score+=1
if choice== "paper" and computer_choice== "scisors":
computer_score+=1
if choice == "paper" and computer_choice == "rock":
player_score+=1
if choice== "scisors" and computer_choice== "rock":
computer_score+=1
if choice == "scisors" and computer_choice == "paper":
player_score+=1
print("round "+ str(round)+ ":" + "player:" +choice + " computer:" + computer_choice + ", score:" +str(player_score) + ":"+str(computer_score))
round+=1
if computer_score==3:
print("computer wins")
break
if player_score==3:
print("player wins")
break

μετα το εκανα με την λογικη 9.3 δεν αλλαξα το tuple με της επιλογες αλλα αλλαξα το πως τα ελγχει

from random import seed
from random import randrange
from datetime import datetime

seed(datetime.now())

choices = ("rock", "paper", "scisors")
player_score = 0
computer_score = 0
round=1
while player_score!=3 or computer_score!=3:
choice = input("rock paper or scisors:")
while choice not in choices:
choice = input("rock paper or scisors:")
if choice=="rock":
numplayer = 0
if choice=="paper":
numplayer = 1
if choice=="scisors":
numplayer = 2
computernum = randrange(0, 3)
print("computer
if
player_score+=1
elif
computer_score+=1
print("round "+ str(round)+ ":" + "player:" +choices[numplayer] + " computer:" + choices[computernum] + ", score:" +str(player_score) + ":"+str(computer_score))
round+=1
if computer_score==3:
print("computer wins")
break
if player_score==3:
print("player wins")
break

ειναι σωστο το δευτερο;

bvilas
Автор

το τελικο μου προγραμμα ειναι:
from random import randrange

wins = 0
elements = ['rock', 'scissor', 'paper']
while wins < 3:
# Εισοδος χρηστη
player = input('chose a element ')
while player not in elements:
player = input('elements are: rock, scissor, paper.Give a element ')

# Τιμη του υπολογιστη
computer = randrange(3)
if computer == 1:
computer = 'rock'
elif computer == 2:
computer = 'scissor'
else:
computer = 'paper'
print(f'computers element is : {computer}')
# Επιλογη νικητη
combinations = {
'rock': 'scissor',
'scissor': 'paper',
'paper': 'rock'
}

if combinations[player] == computer:
print('you won!!')
wins += 1
elif player == computer:
print('Draw!')
else:
print('you lost')
print(f'your scor is {wins} of 3')
pass

gkavliaris_komposkinas
Автор

Η δική μου εκδοχή για το 9.1..Προσπάθησα να το κάνω όσο πιο απλό γίνεται.

from random import seed
from random import randrange
from datetime import datetime

seed(datetime.now())

Game = ['Scissors', 'Rock', 'Paper']
Player = input('Player choose:' )

while Player not in Game:
print('Grapse Swsta re')
Player = input('Player choose:')

x = randrange(len(Game))
Computer = Game[x]
print('Computer choose:' + str(Computer))

if (Computer == 'Rock' and Player == 'Scissors') or (Computer == 'Scissors' and Player == 'Paper') or \
(Computer == 'Paper' and Player == 'Rock' ):
print('Computer Wins')
elif Computer == Player:
print('It is a draw')
else:
print('Player Wins')

ΓιώργοςΠαυλίδης-μο