Hack Forums
[Python] 55 x 2 Dice - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] 55 x 2 Dice (/showthread.php?tid=5731763)



55 x 2 Dice - Will Patterson - Oct 6, 2017

Code
import random

def five():
    reroll = True
    while reroll:
        roll = random.randint(1,100)
        print('You rolled a',roll)
        if roll >= 55:
            print('You rolled over 55! You win!')
        else:
            print('Sorry, you rolled under 55, you lose!')
        print('Do you want to reroll? If yes enter "y" or "yes", if not "n" or "no"')
        reroll = ("y" or "yes") in input().lower()

five()
If anyone has any ideas how I could make it better then please let me know!
Hopefully someone can find this useful


* - vorticalbox - Oct 6, 2017

(Oct 6, 2017 07:12 AM)Will Patterson Wrote:  
Code
import random

def five():
    reroll = True
    while reroll:
        roll = random.randint(1,100)
        print('You rolled a',roll)
        if roll >= 55:
            print('You rolled over 55! You win!')
        else:
            print('Sorry, you rolled under 55, you lose!')
        print('Do you want to reroll? If yes enter "y" or "yes", if not "n" or "no"')
        reroll = ("y" or "yes") in input().lower()

five()
If anyone has any ideas how I could make it better then please let me know!
Hopefully someone can find this useful

I would keep functions to a Single responsibility as per https://en.wikipedia.org/wiki/Single_responsibility_principle

PHP Code

<?php 
from random import randint


def roll
():
  
roll = randint(1,100)
  print(
'You rolled ', roll)
  return 
roll
  
  
def check
(n):
  if 
n > 55:
    return 
True
  
else:
    return 
False

def main
():
  while 
True:
    if 
check(roll()):
      print(
'You rolled over 55 you win!')
    else:
      print(
'Sorry, you rolled under 55, you lose!')
    if 
not input('Would you liek to play again?').lower() == 'y':
      break

main() 



* - koolkid - Oct 6, 2017

(Oct 6, 2017 08:06 AM)vorticalbox Wrote:  I would keep functions to a Single responsibility as per https://en.wikipedia.org/wiki/Single_responsibility_principle

PHP Code

<?php 
from random import randint


def roll
():
  
roll = randint(1,100)
  print(
'You rolled ', roll)
  return 
roll
  
  
def check
(n):
  if 
n > 55:
    return 
True
  
else:
    return 
False

def main
():
  while 
True:
    if 
check(roll()):
      print(
'You rolled over 55 you win!')
    else:
      print(
'Sorry, you rolled under 55, you lose!')
    if 
not input('Would you liek to play again?').lower() == 'y':
      break

main() 

true i always try to keep it simple. it may seem easier to have one function to do everything however one function should have one task


* - vorticalbox - Oct 6, 2017

(Oct 6, 2017 08:51 AM)koolkid Wrote:  true i always try to keep it simple. it may seem easier to have one function to do everything however one function should have one task

Also spelling in prints is a good idea too -.-


* - koolkid - Oct 6, 2017

(Oct 6, 2017 09:18 AM)vorticalbox Wrote:  Also spelling in prints is a good idea too -.-

O.o


* - michalmonday - Oct 6, 2017

hi, ("y" or "yes") isn't going to do the thing, it would have to be either:

Code
reroll = "y" in input().lower() or "yes" in input().lower()

or what I'd prefer:

Code
reroll = input().lower() in ["y", "yes"]



* - vorticalbox - Oct 6, 2017

(Oct 6, 2017 10:03 AM)koolkid Wrote:  O.o

I spelt like wrong in my code lol.


* - koolkid - Oct 6, 2017

(Oct 6, 2017 10:37 AM)vorticalbox Wrote:  I spelt like wrong in my code lol.

lol shit happens .. i need to code something


* - vorticalbox - Oct 6, 2017

(Oct 6, 2017 10:53 AM)koolkid Wrote:  lol shit happens .. i need to code something

A bot the finds all the wrong spellings on threads and posts

*correct spelling


* - koolkid - Oct 6, 2017

(Oct 6, 2017 10:56 AM)vorticalbox Wrote:  A bot the finds all the wrong spellings on threads and posts

*correct spelling

😠 cant bother too tired.. monday to friday work days 😭 saturday is a busy day sunday is rest day .. sigh