Back to 课程

Computer Science GCES EDEXCEL

0% Complete
0/0 Steps
  1. Decomposition And Abstraction Edexcel
    2 主题
  2. Algorithms Edexcel
    11 主题
  3. Truth Tables Edexcel
    3 主题
  4. Binary Edexcel
    6 主题
  5. Data Representation Edexcel
    4 主题
  6. Data Storage And Compression Edexcel
    2 主题
  7. Hardware Edexcel
    5 主题
  8. Software Edexcel
    3 主题
  9. Programming Languages Edexcel
    2 主题
  10. Networks Edexcel
    7 主题
  11. Network Security Edexcel
    2 主题
  12. Environmental Issues Edexcel
    1 主题
  13. Ethical And Legal Issues Edexcel
    3 主题
  14. Cybersecurity Edexcel
    2 主题
  15. Develop Code Edexcel
    6 主题
  16. Constructs Edexcel
    4 主题
  17. Data Types And Data Structures Edexcel
    5 主题
  18. Operators Edexcel
    1 主题
  19. Subprograms Edexcel
    2 主题
课 Progress
0% Complete

Exam code:1CP2

Programming Arithmetic Operators

  • To demonstrate the use of common arithmetic operators, three sample programs written in Python are given below

  • Comments have been included to help understand how the arithmetic operators are being used

    • Arithmetic operators #1 – a simple program to calculate if a user enters number is odd or even

    • Arithmetic operators #2 – a simple program to calculate the area of a circle from a user-inputted radius

    • Arithmetic operators #3 – a simple program that generates 5 maths questions based on user inputs and gives a score of how many were correctly answered at the end

Python examples

# ———————————————————————–
# Arithmetic operators #1
# ———————————————————————–
# Get the user to input a number
user_input = int(input("Enter a number: "))

# if the remainder of the number divided by 2 is 0, the number is even
if user_input % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")

# ———————————————————————–
# Arithmetic operators #2
# ———————————————————————–
# Get the radius from the user
radius = float(input("Enter the radius of the circle: "))

# Calculate the area of the circle
area = 3.14159 * radius ** 2

# Display the calculated area
print("The area of the circle with radius",radius,"is",area)

# ————————————————————————
# Arithmetic operators #3
# ————————————————————————
# Set the score to 0
score = 0

# Loop 5 times
for x in range(5):
num1 = int(input("Enter the first number: "))
operator = input("Enter the operator (+, -, *): ")
num2 = int(input("Enter the second number: "))
user_answer = int(input("What is "+str(num1)+str(operator)+str(num2)+"? "))

# Check the answer and update the score
if operator == '+':
correct_answer = num1 + num2
elif operator == '-':
correct_answer = num1 - num2
elif operator == '*':
correct_answer = num1 * num2

if user_answer == correct_answer:
score = score + 1
else:
print("Sorry that's incorrect.")

print("Your score is:", score)

Programming Boolean & Relational Operators

  • To demonstrate the use of common Boolean operators, three sample programs written in Python are given below

  • Comments have been included to help understand how the Boolean operators are being used

    • Boolean operators #1 – a simple program that assigns Boolean values to two variables and outputs basic comparisons

    • Boolean operators #2 – a simple program to output a grade based on a users score

    • Boolean operators #3 – a simple program reads a text files and searches for an inputted score

Python examples

# ———————————————————–
# Boolean operators #1
# ———————————————————–
# Assign a Boolean value to a and b
a = True
b = False

# print the result of a and b
print("a and b:", a and b)
# print the result of a or b
print("a or b:", a or b)
# print the result of not a
print("not a:", not a)

# ———————————————————–
# Boolean operators #2
# ———————————————————–

# Take input for the score from the user
score = int(input("Enter the score: "))

# Compare the score and output the corresponding grade
if score >= 90 and score <= 100:
print("Grade: A")
elif score >= 80 and score < 90:
print("Grade: B")
elif score >= 70 and score < 80:
print("Grade: C")
elif score < 70:
print("Fail")

# ———————————————————–
# Boolean operators #3
# ———————————————————–
# Open the file for reading
file = open("scores.txt","r")
# Set flags to false
end_of_file = False
found = False
score = input("Enter a score: ")
# While it’s not the end of the file and the score has not been found
while not end_of_file and not found:
  # read the line
scores = file.readline().strip()
# if the line equals the score
if score == str(scores):
found = True
print("Score found")
# if the line is empty
if scores == "":
end_of_file = True
print("Score not found")
file.close()

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注