-5

Here is my code:

def missing_ch(number, string):
      for i in range(len(string)):
        if i==number:
           continue
        else:   
           print(string[i], end=' ')
      return
string='hello'
num=int(input("enter the position of char"))
missing_ch(num, string)

How to get output characters printed in same line?

Andersson
  • 51,635
  • 17
  • 77
  • 129
user5072614
  • 11
  • 1
  • 1
  • 2

3 Answers3

0

I see 3 issues in your program -

  1. In the for loop in second line , you have missed closing a bracket - for i in range (len(str): , it should be - for i in range (len(str)):

  2. There is no casting in python, to convert input to string, you have use int function, as int(...) , so your line (int)(input(...)) should be - int(input(...)) .

  3. In your for loop, you are defining the index as i, but you are using ch inside the loop, you should be using i instead of `ch.

Example -

    for i in range (len(str):
        if(i==num):
            continue
        print(str[i],end=' ')
    return

The print statement to print items without appending the newline is fine, it should be working.

A working example -

>>> def foo():
...     print("Hello",end=" ")
...     print("Middle",end=" ")
...     print("Bye")
...
>>> foo()
Hello Middle Bye
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

To print on one line, you could append each letter on a string.

def missing_ch(num, str):
  result = ""
  for i in range (len(str)):
    if (i == num):
      continue
    result+=str[i]
  print result

string = 'hello'
num = (int)(input('enter the position of char'))

missing_ch(num, string)

#>helo

Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101
-1

I'm new into python and my friend is giving me tasks to perform and one of them was to print a square made out of "*" and this is how I did it:

br = int(input("Enter a number: "))

for i in range(0, br):
    print("*" * br)
Dharman
  • 30,962
  • 25
  • 85
  • 135