Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
coding a 2-player die game
#1
I am still stuck on the dice game I am doing. I was able to get the player and AI into my code, but now I am confused on how to actually get the game into powershell. When I run the program, it throws no errors, but does not do anything. Also, I do not think I did the while loop wrong.

import random
def die():
					self.sides = num_sides
					self.set_value(val)
					player = random.randit(1,6)
					ai=random.randit(1,6)
					print('you have rolled' + str(player))
					if player > ai:
						print('you win')
					else:
						print('you lost')
						print('the computer rolled'+str(ai))
					while True:
						print('please reroll die')
Reply
#2
You need to call your die function for it to do anything. Add die() (with no indentation) to the end of your program.

Your while loop is wrong. Any while True loop without a break in it somewhere will run forever. I'm not sure what you want to do there, so I can't advise on how to fix it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks, I am almost there with the program. Now I am getting an error, that I have no idea why I am getting it. I just updated an issue with the string.

import random
def die():
	def main():
		def __init__(self, sides, num_sides):
					num_sides = sides
					
					self.set_value(val)
					player = random.randit(1,6)
					ai=random.randit(1,6)
					print('you have rolled' + str(player))
					if player > ai:
						print('you win')
					else:
						print('you lost')
						print('the computer rolled'+str(ai))
					while x < 3:
						print(x)
						print('please reroll die')
die()
if __name__ == '__main__':
	main()
Error:
NameError: name 'main' is not defined
Reply
#4
You are mixing up functions and classes here. Note that since main is defined in the die function, you can't see it outside of the die function. That's why you get the error at the end of the program. Also, you would need to call main inside die, and __init__ inside main. Just get rid of main and __init__ for now.

You still have a problem with your while loop. First, you don't define x before the loop, so that will give you an error just like the one you got for main. Second, the value of x never changes in the loop, so the loop will just go on forever. If you want to give the player three chances to beat the ai, I would start by setting x = 0, then the while loop with all of the code from lines 5 through 15 inside it. If you win, use break to get out of the loop; if you lose increase x by one.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tic-Tac game (Beginner's coding) Shahmadhur13 5 3,112 Aug-29-2020, 08:40 PM
Last Post: deanhystad
  Guessing Game with .WAV Files - Python Coding NonEntity 8 4,349 Nov-20-2018, 12:53 AM
Last Post: NonEntity
  Coding Problems With Mr.Stickman Game SheeppOSU 2 4,153 Jun-29-2018, 08:12 PM
Last Post: volcano63
  Game not letting player 1 win straight away mzmingle 6 4,124 Aug-16-2017, 04:18 PM
Last Post: mzmingle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020