How to run my Python 2.7 code on Python 3.0? here is my Python 2.7 code: 1 2 3 4
ID: 3552274 • Letter: H
Question
How to run my Python 2.7 code on Python 3.0?
here is my Python 2.7 code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59def game(army1, army2): prisoner1="" prisoner2="" print "Player 1:",army1," ",prisoner1 print "Player 2:",army2," ",prisoner2 print " " while(len(army1) and len(army2)): if(army1[0]=='H' and army2[0]=='T'): prisoner1="TH"+prisoner2+prisoner1 prisoner2="" army1=army1[1:] army2=army2[1:] elif (army1[0]=='T' and army2[0]=='H'): prisoner2="TH"+prisoner1+prisoner2 prisoner1="" army1=army1[1:] army2=army2[1:] else: prisoner1+=army1[0] prisoner2+=army2[0] army1=army1[1:] army2=army2[1:] if(len(army1)): prisoner1+=army1[0] army1=army1[1:] if(len(army2)): prisoner2+=army2[0] army2=army2[1:] print "Player 1:",army1," ",prisoner1 print "Player 2:",army2," ",prisoner2 print " " if(len(army1) and len(army2)==0): print "Player 1 wins." elif(len(army1)==0 and len(army2)): print "Player 2 wins." else: if(prisoner1.count('H')>prisoner2.count('H')): print "Player 1 wins." elif(prisoner1.count('H')<prisoner2.count('H')): print "Player 2 wins." else: print "Tie." def createRandom(army): opp="" for i in range(len(army)): opp+="HT"[randint(0,1)] return opp total=input("Enter number of games to be played :") for i in range(total): print " Game :",i+1 seq=raw_input("Enter army sequence :") # Change below line to : # game(seq,createRandom(seq)) # if you want to randomly generate opposite army game(seq[:5],seq[5:])
Explanation / Answer
New sourcecode: https://www.dropbox.com/s/17a7jaszbvbgvyi/code_python3.py
Changes made:
1.line 52: convert scanned string to an int
2.line 54: python 3 has no print statement only a print function
3.line 55: python 3 has no raw_input() instead it has input()
4.line 2 : Identation
5.line 4,5,6: print statement
6.line 30,31,32,34,36,39,41,43: print statement
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.