The Student Room Group

Can someone convert my python code to psuedo code please

import random import time import sys from random import randint ##This is how you login## def main(): print ("welcome to the dice game") login() def login(): username="Dave" password="Mmhm" print("Enter username : ") answer1=input() print("Enter password : ") answer2=input() if answer1==username and answer2==password: print("Welcome - Access Granted") menu() def menu(): user1 = input("what is your name player 1 ") user2 = input("what is your name player 2 ") ##This is where i defined all my vairables## def game(): player1 = 0 player2 = 0 player1roll3 = 0 player2roll3 = 0 even = [2, 4, 6, 8, 10, 12, 14, 16, 18] odd = [3, 5, 7, 9, 11, 13, 15, 17] rounds = 0 player1go = 0 player2go = 0 rolling = 0 double = 0 for i in range (5): while True: ##This how player 1 roles## player1 = input("\nPlayer 1 press r to roll") if player1 == "r": print("p1") player1roll1 = random.randint(1, 6) player1roll2 = random.randint(1, 6) player1roll3 = player1roll1 + player1roll2 print("Dice 1 rolled a", player1roll1,) print("Dice 2 rolled a", player1roll2,) print("Player 1 your roll was a", player1roll3,) break ##If player1 roles a double## else: print("\nTo play the game could you please press 'r'") if player1roll1 == player1roll2: print("You got a double. You get one more roll") double = random.randint(1, 6) player1roll3 = player1roll3 + double print("The double dice rolled a", double,) print("You got", player1roll3,) ##If you role a odd number## if player1roll3 in odd: if player1roll3>5: player1go = player1go + player1roll3 - 5 print( "You rolled a odd number 5 points have been removed from your score. You now have", player1go, "points") rolls = 1 droll = 0 rolling = 0 else: print( "You rolled a odd number 5 points have been removed from your score. You now have", player1go, "points") rolls = 1 droll = 0 rolling = 0 ##If you role a even number## elif player1roll3 in even: player1go = player1go + player1roll3 + 10 print( "You rolled a even number 10 points have benn added to your score. You now have", player1go, "points") rolls = 1 droll = 0 rolling = 0 while True: ##This is how player 2 roles## player2 = input("\nPlayer 2 press r to roll") if player2 == 'r': print("P2") player2roll1 = random.randint(1, 6) player2roll2 = random.randint(1, 6) player2roll3 = player2roll1 + player2roll2 print("Dice 1 rolled a", player2roll1,) print("Dice 2 rolled a", player2roll2,) print("Player 2 your roll was a", player2roll3,) rolling = 1 break ##If player 2 roles a double## else: print("\nTo play the game could you please press 'r'") if player2roll1 == player2roll2: print("You got a double. You get one more roll") double = random.randint(1, 6) player2roll3 = player2roll3 + double print("The double dice rolled a", double,) print("You got", player2roll3,) ##If player 2 roles a odd number## if player2roll3 in odd: player2go = player2go + player2roll3 - 5 print( "You rolled a odd number 5 points have been removed from your score. You now have", player2go, "points") rounds = rounds + 1 droll = 1 rolls = 0 rollings = 0 elif player2go in [3, 5]: print( "You rolled a odd number 5 points have been removed from your score. You now have", player2go, "points") rounds = rounds + 1 droll = 1 rolls = 0 rolling = 0 ##If player 2 roles a even number## elif player2roll3 in even: player2go = player2go + player2roll3 + 10 print( "You rolled a even number 10 points have been added to your score. You now have", player2go, "points") rounds = rounds + 1 droll = 1 rolls = 0 rolling = 0 ##If player 1 = player 2 both players role one dice to see who wins## if player1go == player2go: player1 = input("Player 1 press r to roll") if player1 == "r": player1roll1 = random.randint(1, 6) player1roll2 = random.randint(1, 6) player1roll3 = player1roll1 + player1roll2 print("Dice 1 rolled a", player1roll1,) print("Dice 2 rolled a", player1roll2,) print("Player 1 your roll was a", player1roll3,) player2 = input("Player 1 press r to roll") if player2 == "r": player2roll1 = random.randint(1, 6) player2roll2 = random.randint(1, 6) player2roll3 = player2roll1 + player2roll2 print("Dice 1 rolled a", player2roll1,) print("Dice 2 rolled a", player2roll2,) print("Player 1 your roll was a", player2roll3,) ##This shows the final scores## print("\nThe scores have been added up and") time.sleep(2) print('\n') print(player1 , "got", player1go,) print(player2 , "got", player2go,) login() game()
I assume this is an assignment of some sort that you've been given.

These types of posts appear on TSR regularly. If you can't be bothered to attempt your own work, don't expect somebody else to do it for you. You've not even formatted the code to make it legible; you've literally just copied and pasted some sizeable chunk of code for some piece of work you need to do and hoped that someone will make it minimal effort for you.

If you want to talk about how you might approach converting something to pseudocode, myself and others will be more than happy to engage in a discussion to help with the thought process required. But this isn't a 'do my homework for me' forum. :tongue:
The syntax and structure of Python is very human friendly and therefore very similar to a kind of pseudocode, so if you've actually written the code yourself using Python and gotten it working, then translating that into some kind of pseudocode standard should be a trivial exercise akin to simply substituting each line with whatever syntax you want to use for your pseudocode instead.

I would suggest looking at your Python code line-by-line and then trying to match each line with a particular construct whatever pseudocode standard you're trying to follow.

The pseudocode you end up with 'should' look almost exactly the same (merely syntactical differences, nothing else) on a side-by-side comparison, otherwise you've probably done something wrong.

For example, python's if / elif / else structures are probably going to correlate to if..then / else if..then / else. You might also add begin/end in there too, but the overall concepts and structure are exactly the same.
Similarly, your 'for' and 'while' loop constructs in python are probably going to look almost exactly the same as 'for' and 'while' in pseudocode, just with syntactical differences and maybe a begin/end in there too.
(edited 4 years ago)
Can someone convert my python code to pseudocode and make it better too


import time
import sys
import random
import operator
import os
total_score2 = 0
total_score1 = 0
rounds = 0
playerOnePoints = 0
playerTwoPoints = 0
counter = 0

print("*****************Welcome To The DICE Game*******************")
print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user and enter 's' to display scores")
ens=input("")
while ens not in ('e', 'n', 's'): # if anything else but these characters are entered it will loop until it is correct
print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user and enter 's' to display scores")
ens = input()

if ens == "s":
s = open("scores.txt","r")
file_content = s.read().splitlines()
users_points = {i.split()[0]: int(i.split()[2]) for i in file_content}
best_player = max(users_points.items(), key=operator.itemgetter(1))[0]
print("LeaderBoard: ")
print("\n")
print('player with maximum points is {}, this player has {} points'.format(best_player, users_points[best_player]))
best_players = sorted(users_points, key=users_points.get, reverse=True)
for bp in best_players:
print('{} has {} points'.format(bp, users_points[bp])) # This prints all players scores
print("\n")
print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user and enter 's' to display scores")
ens=input("")

if ens == "n":
file = open("accountfile.txt","r+")
text = file.read().strip().split()
check = True
while check:
username=input("Please enter appropiate username: ") #Takes input of a username from user
if username == "": #if no value is entered for the username
continue
if username in text: #username in present in the text file
print("Username is taken please try another one")
else: #username is absent in the text file
print("Username has been accepted")
check = False
check = True
while check:
password1=input("Please enter password: ")
password2=input("Please re-enter password: ")
if password1 == password2:
if password2 in text:
print("Password has been taken please try another one")
else:
print("Username and Password have sucessfully been made Thankyou")
file.write("username: " + username + " " + "password: " + password2 + "\n")
file.close()
check = False
else:
print("passwords do not match please try again")
file.close()

def write1():
print("Player 1 ",username1," Wins!")
file = open("scores.txt","a")
file.write(username1 + " has " + str(total_score1) + " points" + "\n")
file.close()
sys.exit()
def write2():
print("Player 2 ",username2," Wins!")
file = open("scores.txt","a")
file.write(username2 + " has " + str(total_score2) + " points" + "\n")
file.close()
sys.exit()
def validation():
global counter
print("Sorry, this username or password does not exist please try again")
counter = counter + 1
if counter == 3:
print("----------------------------------------------------")
print("You have been locked out please restart to try again")
sys.exit()
def game():
global total_score1
global total_score2
global rounds
global number
global number2
global playerOnePoints
global playerTwoPoints
total_score2 = total_score2 + playerTwoPoints
total_score1 = total_score1 + playerOnePoints
rounds = rounds + 1
number = random.randint(1,6)
number2 = random.randint(1,6)
playerOnePoints = number + number2
print("-------------------------------------------")
print("Round",rounds)
print("-------------------------------------------")
print("Player 1's turn Type 'roll' to roll the dice")
userOneInput = input(">>> ")
if userOneInput == "roll":
time.sleep(1)
print("Player 1's first roll is", number)
print("Player 1's second roll Type 'roll' to roll the dice")
userOneInput = input(">>> ")
if userOneInput == "roll":
time.sleep(1)
print("player 1's second roll is", number2)
if playerOnePoints % 2 == 0:
playerOnePoints = playerOnePoints + 10
print("Player 1's total is even so + 10 points")
print("-------------------------------------------")
print("Player 1 has",playerOnePoints, "points")
else:
playerOnePoints = playerOnePoints - 5
print("player 1's total is odd so -5 points")
print("-------------------------------------------")
print("Player 1 has",playerOnePoints, "points")
number = random.randint(1,6)
number2 = random.randint(1,6)
playerTwoPoints = number + number2
print("-------------------------------------------")
print("Player 2's turn Type 'roll' to roll the dice")
userTwoInput = input(">>> ")
if userTwoInput == "roll":
time.sleep(1)
print("Player 2's first roll is", number)
print("Player 2's second roll Type 'roll' to roll the dice")
userTwoInput = input(">>> ")
if userTwoInput == "roll":
time.sleep(1)
print("player 2's second roll is", number2)
if playerTwoPoints % 2 == 0:
playerTwoPoints = playerTwoPoints + 10
print("Player 2's total is even so + 10 points")
print("-------------------------------------------")
print("Player 2 has",playerTwoPoints, "points")
else:
playerTwoPoints = playerTwoPoints - 5
print("player 2's total is odd so -5 points")
print("-------------------------------------------")
print("Player 2 has",playerTwoPoints, "points")

if ens == "e":
counter = 0
check_failed = True
while check_failed:
print("Could player 1 enter their username and password")
username1=input("Please enter your username ")
password=input("Please enter your password ")
with open("accountfile.txt","r") as username_finder:
for line in username_finder:
if ("username: " + username1 + " password: " + password) == line.strip():
print("you are logged in")
check_failed = False
check_failed = True
while check_failed:
print("Could player 2 enter their username and password")
username2=input("Please enter your username ")
password=input("Please enter your password ")
with open("accountfile.txt","r") as username_finder:
for line in username_finder:
if ("username: " + username2 + " password: " + password) == line.strip():
print("you are logged in")
check_failed = False
time.sleep(1)
print("Welcome to the dice game")
time.sleep(1)
while rounds < 5:
game()
print("-------------------------------------------")
print("Total score for player 1 is", total_score1)
print("-------------------------------------------")
print("Total score for player 2 is", total_score2)
print("-------------------------------------------")
if total_score1 > total_score2:
write1()
if total_score2 > total_score1:
write2()
if total_score1 == total_score2:
print("Its a draw!")
game()
if total_score1 > total_score2:
write1()
if total_score1 < total_score2:
write2()
else:
validation()

else:
validation()

Quick Reply

Latest

Trending

Trending