vendredi 24 juillet 2020

Distribute items from a list into a new list

I have some lists:

original = [1234, 2456, 1245, 5734, 1245, 74512, 13678, 1456, 3926, 1974]
list1 = []
list2 = []
list3 = []
list4 = []

I'm trying to move a certain amount of numbers from the original list into the new lists. list1 should have 4, list2 should have 3, list3 should 2, and list4 should have 1. The numbers that go into the new lists should be picked randomly and not in any particular order. There also shouldn't be any of the same numbers in 2 lists; all lists should have unique numbers. How should I solve this?




Guessing game against the computer Python

I wrote this code for a guessing game against the computer using the random method. The programme work but when it turns to the user for the second round it can not continue. Can you help, please?

import random
global rand_val
rand_val=random.randint(10,50)

def start():
  ans=int(input("Enter a number between 10 and 50: "))
  if ans>=10 and ans<=50:
    new_rand_val=rand_val-ans
    if new_rand_val<=0:
      print("You guessed too far, try again\n")
      main()

    else:
      print("Amount left is,",new_rand_val," Now its the Computer turn\n")
    comput_turn(new_rand_val)

def comput_turn(new_rand_val):
  comp_guess=random.randint(0,new_rand_val) #the comupter is playing by guessing a number
  checking(comp_guess, new_rand_val)
  
def checking(comp_guess, new_rand_val):
    print("\n The computer chose:", comp_guess)#this is for me to follow up
    remain_rand_val=new_rand_val-comp_guess
    if remain_rand_val<=0:
      print("Computer guessed too far, now its trying again:")
      comput_turn(new_rand_val)


      userturn(remain_rand_val)
    print("Amount left is:", remain_rand_val," \n\n Now its your turn\n")
    userturn(remain_rand_val)

def userturn(remain_rand_val):
  ans=int(input("Enter a number between 0 and"))
  print(remain_rand_val)
  
  if ans>=10 and ans<=remain_rand_val:
    user_rand_val=remain_rand_val-ans

    print("Amount left is,",user_rand_val," Now its the Computer turn\n")
    comput_turn(user_rand_val)

def main():
  start()



Random grid path generator

I have a grid consisting of passable and non-passable squares, and need to randomly generate a path. It needs to be something between a maze and a pac-man style map so it has multpile routes between the start and the end and be relatively open but still has dead ends. I've done some research and all I can find is a randomised depth-first search which would make a perfect maze, which I don't want. Here's an example of the sort of path needed:




How do I set a limit for a variable in Python?

import random

dealer = random.randint(10, 21)
begin = random.randint(1, 14)
card = random.randint(1, 14)
limit = int(21)
print(begin)
i = input('Hit or Stay?').lower()

while i == "hit":
    begin += random.randint(1, 14)
    print(begin)
    i = input('Hit or Stay?').lower()

if begin == 21:
    print('Wow, you win!')
elif i == "stay":
    if dealer >= begin:
        print('You lose')
        print(dealer)
    else:
        print('You win')
        print(dealer)
elif begin >= limit:
    print('You lose')

I'm currently trying to make a 21 blackjack game(my own twist ofc). However, I've come across a problem. The rules in blackjack is that if your hand goes over the number 21, you lose. I'm having trouble doing that. Currently, python only prints "you lose!" when the player goes over the number 21 and then stays his hand. Current Output when Reaching 21/Winning:

Starting Hand: 13
Hit or Stay?hit
Current Hand: 15
Hit or Stay?hit
Current Hand: 21
Hit or Stay?stay
Wow, you win!

Expected Output:

Starting Hand: 13
Hit or Stay?hit
Current Hand: 21 
You Win!!!

Current Output for Going Past 21:

Starting Hand:  4
Hit or Stay?hit
Current Hand:  13
Hit or Stay?hit
Current Hand:  24
Hit or Stay?stay
You lose because you went over 21 haha

Expected Output:

Starting Hand: 20
Hit or Stay?hit
Current Hand: 25
You lose because you went over 21 hahaha

I hope someone will be able to fix my code :<




Guessing game using random with two players

I am writing a programme to allow two players taking turns in reducing the random number by guessing any number. For example. The hidden random number is 40, player 1 enter 4, the remainder will be 36..then player 2 turns and so on. The player who leaves 1 as a remaining win. and the player who takes the last 1 loss. The programme is running but I can improve it better

  import random
  def userstart():
  global thisturn #define the variables that will need to be accessed from 
  multiple functions
  global user1
  global user2

  user1=input(print("Enter first player’s name:"))
  user2=input(print("Enter second player’s name: "))
  thisturn=input(print("Who is starting now?: ")) #assign which player to start now
  begin(thisturn, ran_val)

def begin(thisturn, ran_val):
  print("Alright,",thisturn)
  userinput=int(input("Enter number of counter to remove;1 or 2 or 3?:"))
  #take user input and call check input function
  checkiput(ran_val, userinput) 

def checkiput(ran_val, userinput):
  #check if the input is 1,2, or 3 then call calculate() function 
  if userinput==1:calculate(ran_val, userinput)
  elif userinput==2:calculate(ran_val, userinput)
  elif userinput==3:calculate(ran_val, userinput)

def calculate(ran_val, userinput):
  #deduct the user input number from the secret random number
  new_val=ran_val-userinput #assign the result to new variable
  print("amount remain is:", new_val) #this is just for the programmer to follow if this stage is passed
  checknew_val(new_val)

def checknew_val(new_val):#report the result
  while new_val>1:
    ran_val=new_val
    switchuser(thisturn,user1,user2)
    userstart(ran_val)
  if new_val==0:
    print("You took the last counter, you lost!")
    exit()
  elif new_val==1:
    print("There is one counter left, you win!")
    exit()

def switchuser(thisturn,user1,user2):
  #switch between players
  if thisturn==user1:
    thisturn=user2
    print("Now its ",thisturn,"round")
  if thisturn==user2:
    thisturn=user1
    print("Now its ",thisturn,"round")
  begin(thisturn,ran_val)

def main():
  global ran_val #define it as global to be accessible from other function without the need to call the whole function
  ran_val=random.randint(10,50)
  userstart()



Randomly select and assign values to given number of rows in python dataframe

How can I randomly select and assign values to given number of rows in python dataframe. Col B contains only 1's and 0's. Suppose I have a dataframe as below

Col A    Col B
  A        0
  B        0
  A        0
  B        0
  C        0
  A        0
  B        0
  C        0
  D        0
  A        0

I aim to randomly chose 5% of the rows and change the value of Col B to 1. I saw df.sample() but that wont allow me to do inplace changes to the column data




Randomize range function python3.8

I want to create a function randrange(start, stop, step) that every time yields a random number between start and stop (exclusive). The difficult part is that I can't use any lists or other kind of iterators to keep track of already yields number, but only others generator (as range for example) because I want to keep this code light and fast. Any ideas?