Python game - players take turns to subtract from a number - python-3.x

Here is the scenario:
Ask the user for a number which must be between 20 and 30. The user and the computer take it in turns to subtract 1, 2 or 3 from the current value. The last player to subtract a value loses.
Here's what I have so far
import random
while True:
count = int(input("Enter a number between 20 and 30"))
if count < 20 or count > 30:
print("That number is not in range")
else:
print("\nLet's play")
print("\nSubtract 1, 2, or 3 from", count)
# Player moves
def playermove():
while True:
number = int(input("\nWhat number would you like to subtract"))
if number > 1 and number <4:
print("\nyou subtracted", number, "there is", count-number, "left")
print("\nMy turn!")
break
else:
print("\nplease enter 1,2 or 3")
def computermove():
computernum = random.randint(1,3)
print("\nI subtracted", computernum, "there is", count-computernum, "left")
print("\nMake your move")

Get the initial count from the user something like this:
def get_count():
count = 0
while count < 20 or count > 30:
count = int(input("Enter a number between 20 and 30"))
if count < 20 or count > 30:
print("That number is not in range")
else:
return count
count = get_count()
Next, start playing the game.

Related

Python3: How to loop my dice roller program back to the start

Writing my first solo program with no help from teacher or group, a simple code that can act as a D&D dice roller for any type or number of dice a user requires.
I've been working on it for about four hours, and I'm stuck on the last thing I want to do which is loop it back to the beginning instead of just ending when a user doesn't reroll the already chosen dice, I'd like it so it starts again from the top so the player can input a new dice value and number of rolls generated without closing the program and rerunning it.
import random
try:
min = 1
max = int(input("Enter the highest value of dice to be rolled: "))
except:
print("Your input was invalid, program rolled a d20 by default")
min = 1
max = 20
again = True
number_of_dice = int(input("Enter number of dice to roll: "))
for i in range(number_of_dice - 1):
print(random.randint(min, max))
while again:
print(random.randint(min, max))
reroll = input("Roll again? (y/n): ")
if reroll.lower() == "y" or reroll.lower() == "yes":
for i in range(number_of_dice - 1):
print(random.randint(min, max))
else:
print("Thank you")
break
You might try something like:
import random
while True:
try:
min = 1
max = int(input("Enter the highest value of dice to be rolled or 0 to exit: "))
except:
print("Your input was invalid, program rolled a d20 by default")
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int(input("Enter number of dice to roll: "))
for i in range(number_of_dice - 1):
print(random.randint(min, max))
while again:
print(random.randint(min, max))
reroll = input("Roll again? (y/n): ")
if reroll.lower() == "y" or reroll.lower() == "yes":
for i in range(number_of_dice - 1):
print(random.randint(min, max))
else:
print("Thank you")
break
Also, I would suggest renaming "min" and "max" as they are reserved keywords

How can I make the largest number with most numbers of divisors appear in the output?

Title kinda says it all, how do I make a code where I can make the largest number with most divisors appear in the output?
start = int(input("Enter start number: "))
if start <= 0:
print("Invalid Input...")
end = int(input("Enter end number: "))
if end <= 0:
print("Invalid Input...")
divisor = 0
for i in range (start, end):
if end% i == 0:
divisor = i
divisor //= start
print("{0} has {1} divisor.".format(end, divisor))
I expect the output to be like if I input 5 at start and 100 at the end it would look like
Enter start number: 5
Enter end number: 100
96 has 10 divisors.
But instead of 96 it is 100.
Make a function, that will return number of divisors for selected number. That way you can find a number with maximum count of divisors and print it:
start = int(input("Enter start number: "))
if start <= 0:
print("Invalid Input...")
end = int(input("Enter end number: "))
if end <= 0:
print("Invalid Input...")
def divisors(n):
cnt = 0
for i in range(2,int(n**0.5)+1):
if n%i == 0:
cnt += 2
return cnt
current_max, current_cnt = 0, 0
for i in range(start, end):
n = divisors(i)
if n >= current_cnt:
current_cnt = n
current_max = i
print("{0} has {1} divisor.".format(current_max, current_cnt))
Prints (for input 5 and 100):
96 has 10 divisor.
Write a Python program to input 3 numbers and find the largest. Print all the numbers, and the largest among them, with appropriate titles.
n1=float(input("Enter number 1: "))
n2=float(input("Enter number 2: "))
n3=float(input("Enter number 3: "))
if n1 > n2 :
if n1 > n3 :
LNo=n1
else :
LNo=n3
else :
if n2 >n3 :
LNo=n2
else :
LNo=n3
print("DISPLAY........")
print("Number 1 : ",n1)
print("Number 2 : ",n2)
print("Number 3 : ",n3)
print("Largest number is : ",LNo)

How To Limit The Times a User Could Input

Hi I'm a beginner Making a Game Where The Program Generates a random Number And The User Needs To Guess The Number. If They Fail 5 Times They Lose. I Need To Count How Many Times The User Can Enter And If It Reaches 5 And Print You Lost And restart.
Something Like This
cnt = 0
if cnt >= 5:
print("You Lost")
pass
You need :
import random
op_number = random.randint(1,20)
# print(op_number)
number = int(input("Guess a number between 1 to 20 : "))
count=0
while True:
if count == 5:
print("You Lose.")
break
count+=1
if op_number == number:
print("You won")
break
else:
print("Incorrect guess. {} attempts left.".format(5-count))
number = int(input("Guess a number between 1 to 20 : "))

How do I sort out this "NameError"?

def Get_Details():
Student_Name = input("Enter the name of the student: ")
Coursework_Mark = int(input("Enter the coursework mark achieved by the student: "))
while Coursework_Mark < 0 or Coursework_Mark >60:
print("Try again, remember the coursework mark is out of 60.")
Coursework_Mark = int(input("Enter the coursework mark achieved by the student: "))
Prelim_Mark = int(input("Enter the prelim mark achieved by the student: "))
while Prelim_Mark < 0 or Prelim_Mark > 90:
print("Try again, remember the prelim mark is out of 90.")
Prelim_Mark = int(input("Enter the prelim mark achieved by the student: "))
return Student_Name, Coursework_Mark, Prelim_Mark
def Calculate_Percentage(Coursework_Mark, Prelim_Mark):
Percentage = ((Coursework_Mark + Prelim_Mark)/150) * 100
if Percentage >= 70:
Grade = "A"
elif 60 >= Percentage <= 69:
Grade = "B"
elif 50 >= Percentage <= 59:
Grade = "C"
elif 45 >= Percentage <= 50:
Grade = "D"
else:
Grade = "No Award"
return Percentage, Grade
def Display_Results(Student_Name, Grade):
print(Student_Name + " achieved a grade " + str(Grade) + ".")
#MAIN PROGRAM
Student_Name, Coursework_Mark, Prelim_Mark = Get_Details()
Percentage = Calculate_Percentage(Coursework_Mark, Prelim_Mark)
Display_Results(Student_Name, Grade)
At the end of the program I receieve:
Program.py", line 41, in <module>
Display_Results(Student_Name, Grade)
NameError: name 'Grade' is not defined
How can this be fixed? Please help, thank you.
This program asks the user their name, coursework mark (out of 60) and prelim mark (out of 90) and calculates their percentage which is send to their screen as a grade along with their name.
Function Calculate_Percentage returns two values, Percentage and Grade. It looks like you wanted to assign them to a separate variable each exactly like you did with three values from the Get_Details call in the line above.
So last two lines should look like this:
Percentage, Grade = Calculate_Percentage(Coursework_Mark, Prelim_Mark)
Display_Results(Student_Name, Grade)
Please use a Pythonic naming convention to make your code more readable. For example variable names are usually all_lower_case.

Cannot keep certain numbers from averaging

I need help. I'm trying to run the program below. When I enter a number above 100 or below 0 I need it to disregard that input any advice?
total = 0.0
count = 0
data = int(input("Enter a number or 999 to quit: "))
while data != "":
count += 1
number = float(data)
total += number
data = int(input("Enter a number or 999 to quit: "))
try:
data = int(data)
except ValueError:
pass
average = round(total) / count
if data == 999:
break
elif data >= 100:
print("error in value")
elif data <= 0:
number = 0
print("error in value")
print("These", count, "scores average as: ", round(average, 1))
You could move the average = ... line behind the checking for invalid numbers and add continue to the checks for invalid numbers. So the last lines would end up like this:
if data == 999:
break
elif data >= 100:
print("error in value")
continue
elif data <= 0:
print("error in value")
continue
average = round(total) / count

Resources