×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Python Functions and Random Variable Generation: A Sample Assignment Solution

August 31, 2024
Walter Parkes
Walter Parkes
🇺🇸 United States
Python
Walter Parkes is a seasoned Python developer with over a decade of experience in coding and teaching. Specializing in fundamental programming concepts, he excels in creating practical solutions for common assignments, from prime number checks to interactive games and simulations. His expertise helps students master Python efficiently and effectively, enhancing their problem-solving skills.
Key Topics
  • Question:
  • Solution:
Tip of the day
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​

Welcome to our sample solution for a Python assignment, crafted to provide top-notch Python assignment help. In this example, we tackle fundamental programming tasks such as function creation and random variable generation. This assignment demonstrates how to develop a Python function to check for prime numbers, calculate the sum of even numbers, create an interactive guessing game, and simulate a simple dice game. By exploring this sample, you'll see how we approach Python programming challenges with clarity and precision, helping students enhance their coding skills effectively. For more detailed assistance and personalized support, our team is here if you need help with programming assignments.

Question:

Q1: Write a Python function named check_prime() that takes a positive integer as an input from user and determines whether the number entered is prime or composite.

Q2: Write a Python function that asks the user to enter a positive integer n. Using a for loop, calculate and print the sum of all the even numbers from 1 to n.

Q3: Write a Python program that generates a random number between 1 and 100 and asks the user to guess it. Create a loop that continues until the user guesses the correct number. Provide hints like "too high" or "too low" based on the user's input.

For example, random number generated = 76 (use random library to generate random

number)

user inputs: 56

output: “too low”

User inputs: 79

ouput: “too high”

user inputs: 76

ouput: “Number: 76 guessed correctly.”

otherwise, keep outputting “too high” or “too low” until user guesses the correct

number.

Q4: You are tasked with simulating a simple game. In this game, there are two players, Player A and Player B. They take turns rolling a six-sided die. The game continues until one of the players rolls a 6. When a player rolls a 6, they win, and the game ends. (Hint: use random to roll a dice every time)

Write a Python program to simulate this game. Use a while loop to repeatedly simulate die rolls for both Player A and Player B until one of them rolls a 6. Print the name of the winning player.

Solution:

Program 1

def check_prime(number): """ Function to check if a given number is prime """ # Any number below 1 are non-prime if number <= 1: return False # Check that there are no number that is divisible to it except itself for i in range(2, number): if number % i == 0: return False return True def main(): """ Entry point of the program, asks the user to enter a number and then tells if it is prime or composite """ number = int(input("Enter a number: ")) if check_prime(number): print("The number is prime.") else: print("The number is composite.") main()

Program 2

def sum_evens(): """ Ask the user to enter a number, calculate the sum of even numbers from 1 to the said number. """ number = int(input("Enter a number: ")) total = 0 for i in range(1, number + 1): # Consider only to update the total if the current number is even if i % 2 == 0: total += i print("The sum of even numbers from 1 to " + str(number) + " is " + str(total)) sum_evens()

Program 3

import random def main(): """ A game where a random number is generated and the user has to guess it """ # Generate the number to guess target_answer = random.randint(1, 100) answer = 0 # Game begins print("I'm thinking of a number between 1 and 100") while answer != target_answer: answer = int(input("Guess the number: ")) if answer > target_answer: print("Too high.") elif answer < target_answer: print("Too low.") else: print("Correct!") main()

Program 4

import random def main(): """ Simulate a 2 player dice game. Whoever rolls a 6 first wins """ currentTurn = "Player A" rolled_value = random.randint(1, 6) print(currentTurn + " rolled a " + str(rolled_value)) while rolled_value != 6: # Switch turns if currentTurn == "Player A": currentTurn = "Player B" else: currentTurn = "Player A" rolled_value = random.randint(1, 6) print(currentTurn + " rolled a " + str(rolled_value)) # Print the winner print(currentTurn + " wins!") main()

Similar Samples

At Programming Homework Help, we offer expert assistance with all programming assignments. Browse our sample solutions to understand our approach and quality. Rate our work and feel confident in choosing us for your assignment needs. Trust us to help you excel in your programming journey.