100% found this document useful (1 vote)
78 views

UTS Pengantar Python

This document contains instructions for a programming assignment involving determining if a year is a leap year and creating a math quiz game in Python. For the first part, students are asked to write a program that takes a year as input and outputs whether it is a leap year based on criteria of being divisible by 4, 100, or 400. For the second part, students are asked to create a math quiz game with levels of addition, subtraction, and multiplication. The game starts with a level selection menu and presents random problems to answer, tracking lives, score, and providing feedback on correct/incorrect answers.

Uploaded by

Aufa Laila Exo L
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
78 views

UTS Pengantar Python

This document contains instructions for a programming assignment involving determining if a year is a leap year and creating a math quiz game in Python. For the first part, students are asked to write a program that takes a year as input and outputs whether it is a leap year based on criteria of being divisible by 4, 100, or 400. For the second part, students are asked to create a math quiz game with levels of addition, subtraction, and multiplication. The game starts with a level selection menu and presents random problems to answer, tracking lives, score, and providing feedback on correct/incorrect answers.

Uploaded by

Aufa Laila Exo L
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

KEMENTERIAN PENDIDIKAN DAN KEBUDAYAN

UNIVERSITAS SEBELAS MARET


FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
PROGRAM STUDI INFORMATIKA
Jl. Ir. Sutami 36 A Kentingan Surakarta 57126 Telp/Fax. (0271) 66 3375

Mata Kuliah : Konsep Pemrograman (Kelas A) Semester : Genap 2022/2023


Pengampu : Drs. Bambang Harjito, M.App.Sc., Ph.D. Waktu : -
Ujian : UTS Hari, Tanggal : April 2023

1. Soal 1. Hasil Eksekusi Program


Tuliskan keluaran (apa yang tercetak di layar) dari program-program di bawah ini pada kotak kosong di
samping kanan program. Jika tidak ada keluaran, tuliskan: Tidak ada keluaran.

No Program Keluaran
1 def calc_q1(x):
q=4*x+1
return q
calc_q1(5)
print(q)
What output is produced by the print() statement when
the following code is executed?
2 def get_input():
x = float(input("Enter a number: "))
return x
def main():
get_input()
print(x ** 2)
main()

At the prompt the user enters 2. What is the output of


this program?
3 The following code is executed.

def inc_by_two(x):
x=x+2
return x
x = 10
inc_by_two(x)
print("x = ", x)

What is the output produced by the print() statement?

4 What is the output generated by the following code?

a=5
b = -10
if a < b or a < 0 and b < 0:
print("Yes, it’s true.")
else:
print("No, it’s false.")
KEMENTERIAN PENDIDIKAN DAN KEBUDAYAN
UNIVERSITAS SEBELAS MARET
FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
PROGRAM STUDI INFORMATIKA
Jl. Ir. Sutami 36 A Kentingan Surakarta 57126 Telp/Fax. (0271) 66 3375
5 What is the output generated by the following code?
a = -5
b = -10
if (a < b or a < 0) and b < 0:
print("Yes, it’s true.")
else:
print("No, it’s false.")
6 What output is produced by the following code?
def monotonic(xlist):
for i in range(len(xlist) - 1):
if xlist[i] < xlist[i + 1]:
return False
return True
data1 = [5, 3, 2, 2, 0]
data2 = [5, 2, 3, 2, 0]
print(monotonic(data1), monotonic(data2))
7 What output is produced by the following code?
def swapper(xlist):
for i in range(len(xlist) - 1):
if xlist[i] > xlist[i + 1]:
# Swap values.
xlist[i], xlist[i + 1] = xlist[i + 1], xlist[i]
data = [5, 3, 2, 2, 0]
swapper(data)
print(data)
8 What is the value of x after the following code executes?
y = 10
if 2 * 4 - 8:
x=2*4-8
else:
x=y
9 To what value is the variable x set by the following
code?
def multiply_list(start, stop):
product = 1
for element in range(start, stop):
product = product * element
return product
x = multiply_list(1, 4)
10 Consider the following program:
def main():
num = eval(input("Enter a number: "))
for i in range(3):
num = num * 2
print(num)
main()
Suppose the input to this program is 2, what is the
output ?

Suppose the input to this program is 2, what is the


KEMENTERIAN PENDIDIKAN DAN KEBUDAYAN
UNIVERSITAS SEBELAS MARET
FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
PROGRAM STUDI INFORMATIKA
Jl. Ir. Sutami 36 A Kentingan Surakarta 57126 Telp/Fax. (0271) 66 3375

Soal 2. Membuat Program

Tahun kabisat adalah tahun yang bulan Pebruarinya berjumlah 29 hari. Berikut ini adalah ketentuan
2.1 untuk menentukan tahun kabisat atau tidak:

• Jika angka tahun itu habis dibagi 400, maka tahun itu sudah pasti tahun kabisat.
• Jika angka tahun itu tidak habis dibagi 400 tetapi habis dibagi 100, maka tahun itu sudah pasti
bukan merupakan tahun kabisat.
• Jika angka tahun itu tidak habis dibagi 400 dan tidak habis dibagi 100 akan tetapi habis dibagi 4,
maka tahun itu merupakan tahun kabisat.
• Jika angka tahun tidak habis dibagi 400, tidak habis dibagi 100, dan tidak habis dibagi 4, maka
tahun tersebut bukan merupakan tahun kabisat.
(Referensi: https://id.wikipedia.org/wiki/Tahun_kabisat)
Buatlah program Python untuk menentukan apakah suatu tahun merupakan tahun kabisat atau tidak.
Contoh tampilan:
Masukkan Tahun : XXXXX (input)
Tahun XXXX merupakan TAHUN KABISAT (output)

Tampilkan Screen Shot dari hasil code di atas


2.2 Buatlah program game penjumlahan menggunakan Python. Skenario game adalah sbb:
Pertama tampilan yang muncul adalah menu pilihan level:
Menu pilihan level :
1. Level 1 (Penjumlahan)
2. Level 2 (Pengurangan)
3. Level 3 (Perkalian)
4. ExitPilih nomor pilihan: XXX
Jika no menu yang dipilih adalah 1, maka akan muncul serangkaian pertanyaan kuiz tentang penjumlahan
X + Y, di mana X dan Y adalah bilangan bulat acak antara -100 dan 100. Selanjutnya player diminta
menjawab hasil penjumlahannya. Mula-mula jumlah lives player adalah 3, dan skornya 0. Setiap kali
player menjawab benar, maka skor bertambah 2. Sedangkan apabila salah, skornya dikurangi 2 dan
jumlah lives berkurang 1. Apabila player menjawab salah sebanyak 3 kali (livesnya sudah 0), maka game
over. Contoh tampilannya:
Hasil dari 4 + 5 adalah 9
Hore... benar !!! skor Anda 2 (lives: 3)
Hasil dari 10 + 12 adalah 22

You might also like