0% found this document useful (0 votes)
20 views14 pages

Python

The document contains a series of Python programming exercises ranging from basic input/output operations to more complex tasks like checking for prime numbers and creating patterns. It also addresses common issues encountered during coding, such as input errors and recursion bugs, providing solutions and best practices. Additionally, it includes examples of how to implement various algorithms and logic in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

Python

The document contains a series of Python programming exercises ranging from basic input/output operations to more complex tasks like checking for prime numbers and creating patterns. It also addresses common issues encountered during coding, such as input errors and recursion bugs, providing solutions and best practices. Additionally, it includes examples of how to implement various algorithms and logic in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# Store your name, age, and city in variables and print them in one line.

# name = input("Enter you name: ")

# age = int(input("Enter your age: "))

# city = input("Enter your city: ")

# print("Name:", name, "Age:" , age, "City:", city)

# Take a number from the user and print its square.

# n = int(input("Enter a number: "))

# print(n*n)

# Take two numbers and print:

# Sum, Difference, Product, Division

# a = int(input("Enter num: "))

# b = int(input("Enter num: "))

# print("Sus:", a+b, "Sub:", a-b, "Mul:", a*b, (if(b! = 0):"Div:", a%b, "number cannot / by
0"))

# Check whether a number is positive or negative.

# n = int(input("Enter number:"))

# if(n < 0):

# print("number is negative")

# elif(n > 0):

# print("number is positive")

# else:
# print("number is 0")

# LEVEL 2

# Take a number and check whether it is even or odd.

# n = int(input("Enter number:"))

# if(n % 2 == 0):

# print("even")

# else:

# print("odd")

# check voting eligibility

# def takeAge():

# age = int(input("what is your age: "))

# if(age >= 18):

# print("Eligible to vote")

# elif(age < 0):

# print("Enter valid age")

# takeAge()

# else:

# print("not eligible to vote")

# takeAge()

# Take 3 numbers and print the largest one


# a = int(input("num1: "))

# b = int(input("num2: "))

# c = int(input("num3: "))

# if(a>b and a>c):

# print("a greater")

# elif(b>a and b>c):

# print("b is greater")

# else:

# print("c is greater")

# Take marks (0–100) and print:

# ≥90 → A

# ≥75 → B

# ≥50 → C

# Else → Fail

# a = int(input("num1: "))

# if(a >= 90): print("A")

# elif(a >= 75): print("B")

# elif(a >= 50): print("C")

# else: print("FAIL")

# Take a number and print numbers from 1 to that number using a loop.

# n = int(input("Enter number: "))

# for i in range(1, n+1):

# print(i)
# LEVEL 3 — Medium (Real Practice)

# Print all even numbers from 1 to 50

# for i in range(1, 51):

# if(i%2==0):

# print(i)

# Take a number and print its multiplication table.

# n = int(input("enter the number : "))

# for i in range(1, 11):

# print(n*i)

# Count how many digits are in a number.

# n = int(input("enter the number : "))

# count = 0

# while n != 0:

# n = n // 10

# count += 1

# print(count)

# Reverse a number.

# n = int(input("enter the number : "))


# copy = 0

# while n != 0:

# tmp = n % 10

# copy = (copy * 10) + tmp

# n = n //10

# print(copy)

# Check whether a number is palindrome

# n = int(input("enter the number : "))

#x=n

# copy = 0

# while n != 0:

# tmp = n % 10

# copy = (copy * 10) + tmp

# n = n // 10

# print(copy)

# if (x == copy): print("Palindrome")

# else: print("Not Palindrome")

# *************************************************

# LEVEL 4 — Tricky (Think Step-by-Step)

# Guess the number game:

# Store a secret number

# Keep asking until user guesses correctly


# import random

# x = [Link](1, 10)

# def play():

# n = int(input("enter the number : "))

# while n != x:

# print("try again")

# n = int(input("enter the number : "))

# play()

# print("Great!!! Correct number...")

# Print this pattern:

#*

# **

# ***

# ****

# *****

# for i in range(1, 6):

# print("*"*i)

# Print this pattern:

# *****
# ****

# ***

# **

#*

# for i in range(6, 0, -1):

# print("*"*i)

# Count how many even and odd numbers are between 1 and 100.

# countEven = 0

# countOdd = 0

# for i in range(1, 101):

# if(i % 2 == 0):

# countEven += 1

# else:

# countOdd += 1

# print("Even:", countEven, "Odd:", countOdd)

# Take a number and check if it is prime.

# n = int(input("enter the number : "))

# flag = False

# for i in range(2, n):

# if(n % i == 0):

# break

# else:

# flag = True
# if(flag == True): print("prime")

# else: print("Not Prime")

# Print numbers from 1-100:

# Replace multiples of 3 with “Fizz”

# Multiples of 5 with “Buzz”

# Multiples of both with “FizzBuzz”

# for i in range(1, 101):

# if(i%3 == 0 and i%5 == 0):

# print("FizzBuzz")

# elif(i%3 == 0):

# print("Fizz")

# elif(i%5 == 0):

# print("Buzz")

# else:

# print(i)

y = [10]

print(dict(y))

The issue is that your input is somehow capturing extra text beyond just "1234". Looking
at the error message:

ValueError: invalid literal for int() with base 10: '1234python -u


"d:\\Python\\[Link]"'

CopyInsert at cursor

The input received was

'1234python -u "d:\\Python\\[Link]"'

instead of just
'1234'

This typically happens when:

1. You're pasting text that includes the command prompt - Make sure you're only
typing the number when prompted

2. Terminal/IDE issue - The terminal might be echoing commands incorrectly

However, there's also a logic bug in your palindrome code at line 135:

n = int(input("enter the number : "))

copy = 0

while n != 0:

tmp = n % 10

copy = (copy * 10) + tmp

n = n // 10

print(copy)

if (n == copy): print("Palindrome") # ← BUG: n is now 0!

else: print("Not Palindrome")

CopyInsert at cursorpython

After the while loop,

becomes

, so you're comparing

0 == copy

, which will always be false (unless the original number was 0).

Fix: Store the original number before modifying it:

n = int(input("enter the number : "))

original = n
copy = 0

while n != 0:

tmp = n % 10

copy = (copy * 10) + tmp

n = n // 10

print(copy)

if (original == copy): print("Palindrome")

else: print("Not Palindrome")

CopyInsert at cursorpython

To fix the immediate input issue: Just type the number cleanly without any extra
characters when prompted. If the problem persists, try running the script in a fresh
terminal window.

PS D:\Python> python -u "d:\Python\[Link]"


enter the number : 1234python -u "d:\Python\[Link]"
Traceback (most recent call last):
File "d:\Python\[Link]", line 135, in <module>
n = int(input("enter the number : "))
ValueError: invalid literal for int() with base 10: '1234python -u
"d:\Python\[Link]"'
what the issue
what was this issue

The issue is that when you typed "1234" at the input prompt, somehow the command

python -u "d:\Python\[Link]"

got appended to your input, making it:

'1234python -u "d:\\Python\\[Link]"'

CopyInsert at cursor

This is not a code issue - it's a terminal/input issue. This typically happens due to:

1. Accidental paste - You may have accidentally pasted the command along with
the number

2. Terminal buffer issue - The terminal had leftover text in its buffer
3. Keyboard/input glitch - Some keys triggered unexpected behavior

How to avoid this:

• Clear the terminal before running: Type

cls

(Windows) or

clear

(Linux/Mac)

• Type carefully - Only type the number when prompted, don't copy-paste

• Restart the terminal if the issue persists

• Run the script fresh - Close and reopen your terminal/VS Code

Your code is now correct (you fixed the palindrome logic by storing

x=n

), so just run it again and type only the number when prompted.

Traceback (most recent call last):


File "d:\Python\[Link]", line 165, in <module>
File "d:\Python\[Link]", line 162, in play
while n != x:
^^^^^^
File "d:\Python\[Link]", line 162, in play
while n != x:
^^^^^^
File "d:\Python\[Link]", line 162, in play
while n != x:
^^^^^^
[Previous line repeated 8 more times]
File "d:\Python\[Link]", line 159, in play
ValueError: invalid literal for int() with base 10: 'python -u "d:\Python\[Link]"'
PS D:\Python> python -u "d:\Python\[Link]"

You have two issues:

1. Same terminal input problem - The command is still getting mixed with your
input

2. Infinite recursion bug in your guessing game code (lines 159-163)

The Recursion Bug


Your

play()

function calls itself recursively, which causes a stack overflow. The traceback shows

[Previous line repeated 8 more times]

- that's the recursion limit being hit.

Fix: Use a loop instead of recursion:

import random

x = [Link](1, 10)

def play():

n = int(input("enter the number : "))

while n != x:

print("try again")

n = int(input("enter the number : "))

play()

print("Great!!! Correct number...")

CopyInsert at cursorpython

Or simpler without a function:

import random

x = [Link](1, 10)

n = int(input("enter the number : "))

while n != x:

print("try again")

n = int(input("enter the number : "))

print("Great!!! Correct number...")


CopyInsert at cursorpython

The Terminal Issue

For the input problem, close VS Code completely and reopen it. Something is
interfering with your terminal's input buffer. After reopening, run the script fresh.

import random

x = [Link](1, 10)

def play():
n = int(input("enter the number : "))
while n != x:
print("try again")
play()

play()
print("Great!!! Correct number...")
wht is issue

The issue is infinite recursion. When the user guesses wrong, you call

play()

again, which creates a new function call on top of the previous one. This keeps stacking
up until Python hits its recursion limit and crashes.

The problem:

while n != x:

print("try again")

play() # ← Creates infinite nested calls

CopyInsert at cursorpython

The fix - Ask for input again inside the loop instead of calling the function:

import random

x = [Link](1, 10)

def play():

n = int(input("enter the number : "))


while n != x:

print("try again")

n = int(input("enter the number : ")) # ← Ask again here

play()

print("Great!!! Correct number...")

CopyInsert at cursorpython

This way, the loop keeps asking for new input without creating nested function calls.

You might also like