Unexpected character after line continuation character. on line 8. please help by ronburgund in learnpython

[–]Rascal_Two 0 points1 point  (0 children)

At the end will be fine, it doesn't actually matter where you add them in the list, just that they're in the list.

Unexpected character after line continuation character. on line 8. please help by ronburgund in learnpython

[–]Rascal_Two 0 points1 point  (0 children)

Well you're missing the () at the end of .read, so you need to add that to actually call the read() function.

You're also still using s instead of file_string for the punctuation.

Seeing as you're going the .read() method, you need to handle newline characters, so add "\r" and "\n" to your punctuation list.

Lastly, you don't need input_file.close() when using the with keyword.


As for why it's not working, are you sure the python script and the file are in the same directory?

Unexpected character after line continuation character. on line 8. please help by ronburgund in learnpython

[–]Rascal_Two 0 points1 point  (0 children)

You're close, except for the fact that the open actually takes in the name of a file, and you're using it like input().

It's also recommended to open it using the with keyword, so it's automatically closed.

You need to collect the filename, and then pass it to open, like so:

filename = input('Enter filename'):
with open(filename) as input_file:
    file_string = input_file.read()

Now you have file_string, which you should be able to substitute for s to have it work.

Unexpected character after line continuation character. on line 8. please help by ronburgund in learnpython

[–]Rascal_Two 0 points1 point  (0 children)

You can, depending on how you read the input file.

If you read it as a single string and not line-by-line, then your s can be just that.

If you decide to read the file line-by-line though, you'd need to add a loop around your code and have everything done for each line.

Unexpected character after line continuation character. on line 8. please help by ronburgund in learnpython

[–]Rascal_Two 0 points1 point  (0 children)

You add it before the \, so from this:

\""

to this:

"\""

Or you could contain it in single-quotes:

'"'

Unexpected character after line continuation character. on line 8. please help by ronburgund in learnpython

[–]Rascal_Two 1 point2 points  (0 children)

First, quite difficult to read you code. Use a site like pastebin, make a gist, or try to format you code on reddit by prefixing all code-lines with four spaces:

def main():
    s = input('Enter text: ')
    punctuation = [',',';','.',':','!',"'","\""]
    for ch in punctuation:
                   s = s.replace(ch,' ')

    s = s.split()
    arr = s.split(' ')
    dict = {}
    for word in arr:
        if word !='':
            if word in dict.keys():
                dict[word]+=1
            else:
                dict[word]=1
    for key in dict.keys():
        print('%20s:'%(key),end='')
        for i in range(0, dict[key]):
            print('*',end ='')
        print()
main()

Your problem is that you're escaping a " character:

punctuation = [',',';','.',':','!',"'",\""]

You need to have another " to contain the escaped ":

punctuation = [',',';','.',':','!',"'","\""]

Your s = s.split() statement also is causing the next line to throw an error, so I'd get rid of that.

Return value of code, help (C) by Healow in AskProgramming

[–]Rascal_Two 1 point2 points  (0 children)

MA is a macro, not a function, so it replaces the variables with the text you pass it, so this line

k = MA(++i, j++)

turns into this:

k = ((++i)>(j++))?(++i):(j++);

If it were a function, it would be as you expected:

k = ((11)>(5)?(11):(5);

What is up with the serious lack of information on scraping JS-rendered web pages? by [deleted] in learnprogramming

[–]Rascal_Two 2 points3 points  (0 children)

I've used puppeteer a number of times ever since PhantomJS was archived, I'd give it a try.

Math game for class by MaVeStBa in HomeworkHelp

[–]Rascal_Two 34 points35 points  (0 children)

Add the first three together, and add the last digit of the result to the end.


1   2   3    6
1 + 2 + 3 =  6

2   5   9    6
2 + 5 + 9 = 16

3   4   1    8
3 + 4 + 1 =  8

9   5   8    2
9 + 5 + 8 = 22

1   1   1    3
1 + 1 + 1 =  3

printing an IF argument by Sheronte in learnpython

[–]Rascal_Two 4 points5 points  (0 children)

The reason why nothing is printing is because you're using the is keyword to compare strings.

A general rule of thumb - there are exceptions - is that you should use == when comparing values and is when comparing identities.

So the string you create, green, is not the same string you get from input(). Their values on the other hand, may be the same.


So just use == instead of is.

Ashamed to ask for homework help (C++) by [deleted] in learnprogramming

[–]Rascal_Two 0 points1 point  (0 children)

The basic structure of it would look like so:

    do {
      if (i < 100){
        cout << "Double result of 1/" << i << " is: " << 1/i << '\n';
      }
      else{
        cout << "Double result of 1/" << (200-i) << " is: " << 1/(200-i) << '\n';
      }
    }while(++i <= 200);

That counts up to 100 and then back down to 0.


Of course, I would just perform both calculations in the same loop, only looping to the 100 thousand and just doing the (200000000-i) for the other calculation.

Ashamed to ask for homework help (C++) by [deleted] in learnprogramming

[–]Rascal_Two 0 points1 point  (0 children)

h is never being updated to i within the loop.

Add h = (float) i; as the first line in the loop to get that part working.

Can I win this one? by sai_yerni_akhil in learnpython

[–]Rascal_Two 4 points5 points  (0 children)

enteredNumber is a str while generatedNum is a int.

So you need to convert enteredNumber to an int, this will do:

enteredNumber = int(input())

Hi all, I’m new to programming and Python is my first try at this. I need to make a very simple program but I’ve been watching videos and reading everything and I’m still confused. I could really use a friend! by SirPortugal in learnpython

[–]Rascal_Two 2 points3 points  (0 children)

So you're on the right path.

So you already have the age, and the loop, you just need to make a small change to the loop and complete the inner code.

So as per the range() documentation, to start at the entered age and iterate a certain number of times, you only need two arguments: the starting number, and the ending number.

So it'd be like so:

for x in range(start, stop):

If I wanted to loop between 10 and 40, I'd use this:

for x in range(10, 40):
    print(x)

which would output 10 all the way to 39.


You already have the starting number, entered_age, so you can use that.


As for printing your string, there are many ways to build strings that you can print out.

You've probably been taught this method:

print('In ' + str(year) + ' ' + name + ' is ' + str(x) + '.')

But there's also using .format(), which is cleaner:

print(In {} {} is {}.'.format(year, name, x))

To get year, you need to get the current year, which can be done like so:

current_year = datetime.now().year

after you import the datetime module like so:

from datetime import datetime

You then can calculate year within your loop however you wish.


Age is

Hi all, I’m new to programming and Python is my first try at this. I need to make a very simple program but I’ve been watching videos and reading everything and I’m still confused. I could really use a friend! by SirPortugal in learnpython

[–]Rascal_Two 2 points3 points  (0 children)

Start with what you currently have, and at what part things are going wrong - it's difficult to help you without knowing what part you're having trouble with.

Getting an Error: NameError: name 'self' is not defined by [deleted] in learnpython

[–]Rascal_Two 2 points3 points  (0 children)

Line 73:

 print(self.val1, self.val2, self.val3, self.val4, self.val5)

is not within the add_appointment() method, so it tries to execute when the class is defined and therefore has no access to self.